【问题标题】:How to get src="" from html image tag如何从 html 图像标签中获取 src=""
【发布时间】:2011-12-14 18:36:52
【问题描述】:
$str = "html code here and img tag";
preg_match_all('~<img ([^>]+)>~i', $str, $matches);
$images = array();
foreach ($matches[1] as $str) {
    preg_match_all('~([a-z]([a-z0-9]*)?)=("|\')(.*?)("|\')~is', $str, $pairs);
}

我无法从 IMG 获得 SRC,如何获得想法?

【问题讨论】:

标签: php


【解决方案1】:

我建议您考虑使用simplehtmldom,而不是使用正则表达式解析您的 img src 标签。

$html = file_get_html('...html...');

// Find all images & echo src
foreach($html->find('img') as $element)
       echo $element->src . '<br>';

【讨论】:

    【解决方案2】:

    您可以在 PHP 中使用 XML 解析器,例如 Simple HTML Dom,或者类似的正则表达式也应该可以工作。

    /src="([^"]*)"/i
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多