php 从字符串中获取所有图像 url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1513418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:53:09  来源:igfitidea点击:

Get all images url from string

phphtmlstringimageparsing

提问by ilija veselica

Possible Duplicate:
How to extract img src, title and alt from html using php?

可能的重复:
如何使用 php 从 html 中提取 img src、title 和 alt?

Hi,
I have found solution to get first image from string:

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

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

But I can't manage to get all images from string.
One more thing... If image contains alternative text (altattribute) how to get it too and save to another variable?
Thanks in advance,
Ilija

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

采纳答案by ilija veselica

This is what I tried but can't get it print value of src

这是我尝试过的,但无法获得 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 />';
    }

EDIT: I solved this problem

编辑:我解决了这个问题

$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>";
    }

回答by cletus

Don't do this with regular expressions. Instead, parse the HTML. Take a look at Parse HTML With PHP And DOM. This is a standard feature in PHP 5.2.x (and probably earlier). Basically the logic for getting images is roughly:

不要用正则表达式这样做。相反,解析 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');
}

This should be trivial to adapt to finding images.

这应该很容易适应查找图像。

回答by John Carter

Note that Regular Expressions are a bad approach to parsing anything that involves matching braces.

请注意,正则表达式对于解析涉及匹配大括号的任何内容是一种糟糕的方法。

You'd be better off using the DOMDocumentclass.

最好使用DOMDocument类。

回答by Lars D

You assume that you can parse HTML using regular expressions. That may work for some sites, but not all sites. Since you are limiting yourself to only a subset of all web pages, it would be interesting to know how you limit yourself... maybe you can parse the HTML in a quite easy way from php.

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

回答by Per ?stlund

Look at preg_match_all to get all matches.

查看 preg_match_all 以获取所有匹配项。