duanchong3075 2011-11-01 20:35
浏览 12
已采纳

创建矢量并获取所有图像?

I need some help, geting all the images in my article. At the moment to get only the first image i use this :

$first_img = '';
$mycontent = $row['post_content'];
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $my1content, $matches); 
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$first_img = "/img/default.png";}

This get only the first image from the article..

So what i need it , to get all the images from the article, and show them in a row.

Thank you

  • 写回答

1条回答 默认 最新

  • douzhan4522 2011-11-01 20:40
    关注

    The preg_match_all function will return an array matches, foreach loop would be sufficient.

    http://php.net/manual/en/function.preg-match-all.php

    foreach($matches as $val) {
        echo $val;
    }
    

    Better pattern: /<img(?:(?!src).)+src="?([^"\']+)/i

    To handle the multiple matches you can do the following:

    $mycontent = '<img something="null" src="ggggg.gif"><br/><img src="bob.jpg">';
    $output = preg_match_all('/<img(?:(?!src).)+src="?([^"\']+)/i', $mycontent, $matches, PREG_SET_ORDER); // find all src attributes
    foreach($matches as $val) { //loop over <img> tags matches
        echo $val[1];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办