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];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 求给定范围的全体素数p的(p-2)的连乘积
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页