从字符串中获取所有图像网址

时间:2009-10-03 10:27:09

标签: php html string image parsing

  

可能重复:
  How to extract img src, title and alt from html using php?

您好,
我找到了从字符串中获取第一张图像的解决方案:

preg_match('~<img[^>]*src\s?=\s?[\'"]([^\'"]*)~i',$string, $matches);

但是我无法从字符串中获取所有图像 还有一件事......如果图像包含替代文本(alt属性),如何获取它并保存到另一个变量? 提前谢谢,
伊利亚·

5 个答案:

答案 0 :(得分:32)

不要使用正则表达式执行此操作。而是解析HTML。看看Parse HTML With PHP And DOM。这是PHP 5.2.x中的标准功能(可能更早)。基本上,获取图像的逻辑大致是:

$dom = new domDocument;
$dom->loadHTML($html);
$dom->preserveWhiteSpace = false;
$images = $dom->getElementsByTagName('img');
foreach ($images as $image) {
  echo $image->getAttribute('src');
}

这对于适应寻找图像应该是微不足道的。

答案 1 :(得分:8)

这是我尝试但无法获得src的打印值

 $dom = new domDocument;

    /*** load the html into the object ***/
    $dom->loadHTML($html);

    /*** discard white space ***/
    $dom->preserveWhiteSpace = false;

    /*** the table by its tag name ***/
    $images = $dom->getElementsByTagName('img');

    /*** loop over the table rows ***/
    foreach ($images as $img)
    {
        /*** get each column by tag name ***/
        $url = $img->getElementsByTagName('src');
        /*** echo the values ***/
        echo $url->nodeValue;
        echo '<hr />';
    }

编辑:我解决了这个问题

$dom = new domDocument;

/*** load the html into the object ***/
$dom->loadHTML($string);

/*** discard white space ***/
$dom->preserveWhiteSpace = false;

$images = $dom->getElementsByTagName('img');

foreach($images as $img)
    {
        $url = $img->getAttribute('src');   
        $alt = $img->getAttribute('alt');   
        echo "Title: $alt<br>$url<br>";
    }

答案 2 :(得分:2)

请注意,正则表达式是解析涉及匹配大括号的任何内容的不好方法。

最好使用DOMDocument课程。

答案 3 :(得分:0)

您假设您可以使用正则表达式解析HTML。这可能适用于某些网站,但不适用于所有网站。因为你只限于所有网页的一个子集,所以知道你如何限制自己会很有趣......也许你可以从php中以一种非常简单的方式解析HTML。

答案 4 :(得分:0)

查看preg_match_all以获取所有匹配。