duanben1909 2012-09-16 21:38
浏览 105
已采纳

使用preg_match_all从字符串中提取Image SRC

I have a string of data that is set as $content, an example of this data is as follows

This is some sample data which is going to contain an image in the format <img src="http://www.randomdomain.com/randomfolder/randomimagename.jpg">.  It will also contain lots of other text and maybe another image or two.

I am trying to grab just the <img src="http://www.randomdomain.com/randomfolder/randomimagename.jpg"> and save it as another string for example $extracted_image

I have this so far....

if( preg_match_all( '/<img[^>]+src\s*=\s*["\']?([^"\' ]+)[^>]*>/', $content, $extracted_image ) ) {
$new_content .= 'NEW CONTENT IS '.$extracted_image.'';

All it is returning is...

NEW CONTENT IS Array

I realise my attempt is probably completly wrong but can someone tell me where I am going wrong?

  • 写回答

3条回答 默认 最新

  • dongqian5639 2018-11-16 23:21
    关注

    Using regex to parse valid html is ill-advised. Because there can be unexpected attributes before the src attribute, because non-img tags can trick the regular expression into false-positive matching, and because attribute values can be quoted with single or double quotes, you should use a dom parser. It is clean, reliable, and easy to read.

    Code: (Demo)

    $string = <<<HTML
    This is some sample data which is going to contain an image
    in the format <img src="http://www.randomdomain.com/randomfolder/randomimagename.jpg">.
    It will also contain lots of other text and maybe another image or two
    like this: <img alt='another image' src='http://www.example.com/randomfolder/randomimagename.jpg'>
    HTML;
    
    $srcs = [];
    $dom=new DOMDocument;
    $dom->loadHTML($string);
    foreach ($dom->getElementsByTagName('img') as $img) {
        $srcs[] = $img->getAttribute('src');
    }
    
    var_export($srcs);
    

    Output:

    array (
      0 => 'http://www.randomdomain.com/randomfolder/randomimagename.jpg',
      1 => 'http://www.example.com/randomfolder/randomimagename.jpg',
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 对于这个复杂问题的解释说明
  • ¥50 三种调度算法报错 采用的你的方案
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败