从字符串中查找图像链接,该链接由标签包裹



我需要提取图像链接以用作DIV的背景图像,使用

<a href="http://heartymagazine.com/wp-content/uploads/2014/07/watch-derek-jeter-jordan-brand-video.png"><img src="http://heartymagazine.com/wp-content/uploads/2014/07/watch-derek-jeter-jordan-brand-video.png" alt="" title="watch-derek-jeter-jordan-brand-video" width="270" height="170" class="alignnone size-full wp-image-73989" /></a>

php。我无法处理正则表达式来提取图像URL。请帮我。

<a href="http://heartymagazine.com/wp-content/uploads/2014/07/watch-derek-jeter-jordan-brand-video.png"><img src="http://heartymagazine.com/wp-content/uploads/2014/07/watch-derek-jeter-jordan-brand-video.png" alt="" title="watch-derek-jeter-jordan-brand-video" width="270" height="170" class="alignnone size-full wp-image-73989" /></a>

一种好习惯是从HTML提取信息时使用DOM解析器而不是正则表达式。

一种可能的解决方案:

  1. 将HTML读取到单纯的对象
  2. 运行xpath查询以查找所有img标签
  3. 获取第一个img标签的src属性值

代码:

$html = '<a href="http://heartymagazine.com/wp-content/uploads/2014/07/watch-derek-jeter-jordan-brand-video.png"><img src="http://heartymagazine.com/wp-content/uploads/2014/07/watch-derek-jeter-jordan-brand-video.png" alt="" title="watch-derek-jeter-jordan-brand-video" width="270" height="170" class="alignnone size-full wp-image-73989" /></a>';
// Long version
$dom        = new SimpleXMLElement($html);
$images     = $dom->xpath('//img');
$firstImage = $images[0];
$src        = $firstImage['src'];
// Short version
$src = (new SimpleXMLElement($html))->xpath('//img')[0]['src'];

$src然后将包含(两个版本):

http://heartymagazine.com/wp-content/uploads/2014/07/watch-derek-jeter-jordan-brand-video.png

尝试以下:

$input $ = '<a href="http://heartymagazine.com/wp-content/uploads/2014/07/watch-derek-jeter-jordan-brand-video.png"><img src="http://heartymagazine.com/wp-content/uploads/2014/07/watch-derek-jeter-jordan-brand-video.png" alt="" title="watch-derek-jeter-jordan-brand-video" width="270" height="170" class="alignnone size-full wp-image-73989" /></a>';
$regexp = "<img[^']*?src="([^']*?)"[^']*?>"; 
if(preg_match_all("/$regexp/siU", $input, $matches)) {
   var_dump($matches[1]);
}

一种非常粗糙的解决方案...

    $s='<a href="http://heartymagazine.com/wp-content/uploads/2014/07/watch-derek-jeter-jordan-brand-video.png"><img src="http://heartymagazine.com/wp-content/uploads/2014/07/watch-derek-jeter-jordan-brand-video.png" alt="" title="watch-derek-jeter-jordan-brand-video" width="270" height="170" class="alignnone size-full wp-image-73989" /></a>';
    preg_match('@"(http://.*)"@',$s,$m);
    echo '<pre>';
    echo str_replace(array('"','>'),'',$m[1]);
    echo '</pre>';

最新更新