使用 PHP 获取 img src

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10130858/
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-24 21:27:34  来源:igfitidea点击:

Get img src with PHP

phphtmlimagevariablessrc

提问by pangi

I would like to get the SRC attribute into a variable in this example:

在此示例中,我想将 SRC 属性放入一个变量中:

<img border="0" src="/images/image.jpg" alt="Image" width="100" height="100" />

So for example - I would like to get a variable $foo = "/images/image.jpg". Important! The src attribute will be dynamic, so it mustn't be hardcoded. Is there any quick and easy way to do this?

例如 - 我想得到一个变量$foo = "/images/image.jpg"。重要的!src 属性将是动态的,所以它不能被硬编码。有什么快速简便的方法可以做到这一点?

Thanks!

谢谢!

EDIT: The image will be a part of a huge string that is basically the content of a news story. So the image is just a part of that.

编辑:图像将是一个巨大的字符串的一部分,基本上是新闻故事的内容。所以图像只是其中的一部分。

EDIT2: There will be more images in this string, and I would only want to get the src of the first one. Is this possible?

EDIT2:此字符串中会有更多图像,我只想获取第一个图像的 src。这可能吗?

回答by hakre

Use a HTML parser like DOMDocumentand then evaluate the value you're looking for with DOMXpath:

使用 HTML 解析器DOMDocument,然后使用以下方法评估您要查找的值DOMXpath

$html = '<img id="12" border="0" src="/images/image.jpg"
         alt="Image" width="100" height="100" />';

$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
$src = $xpath->evaluate("string(//img/@src)"); # "/images/image.jpg"

Or for those who really need to save space:

或者对于那些真正需要节省空间的人:

$xpath = new DOMXPath(@DOMDocument::loadHTML($html));
$src = $xpath->evaluate("string(//img/@src)");

And for the one-liners out there:

对于那里的单线:

$src = (string) reset(simplexml_import_dom(DOMDocument::loadHTML($html))->xpath("//img/@src"));

回答by anubhava

You would be better off using a DOM parser for this kind of HTML parsing. Consider this code:

您最好使用 DOM 解析器进行这种 HTML 解析。考虑这个代码:

$html = '<img id="12" border="0" src="/images/image.jpg"
         alt="Image" width="100" height="100" />';
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html); // loads your html
$xpath = new DOMXPath($doc);
$nodelist = $xpath->query("//img"); // find your image
$node = $nodelist->item(0); // gets the 1st image
$value = $node->attributes->getNamedItem('src')->nodeValue;
echo "src=$value\n"; // prints src of image

OUTPUT:

输出:

src=/images/image.jpg

回答by Torsten

I have done that the more simple way, not as clean as it should be but it was a quick hack

我用更简单的方法做到了这一点,不像它应该的那样干净,但它是一个快速的黑客

$htmlContent = file_get_contents('pageURL');

// read all image tags into an array
preg_match_all('/<img[^>]+>/i',$htmlContent, $imgTags); 

for ($i = 0; $i < count($imgTags[0]); $i++) {
  // get the source string
  preg_match('/src="([^"]+)/i',$imgTags[0][$i], $imgage);

  // remove opening 'src=' tag, can`t get the regex right
  $origImageSrc[] = str_ireplace( 'src="', '',  $imgage[0]);
}
// will output all your img src's within the html string
print_r($origImageSrc);

回答by kba

I know people say you shouldn't use regular expressions to parse HTML, but in this case I find it perfectly fine.

我知道人们说你不应该使用正则表达式来解析 HTML,但在这种情况下,我发现它非常好。

$string = '<img border="0" src="/images/image.jpg" alt="Image" width="100" height="100" />';
preg_match('/<img(.*)src(.*)=(.*)"(.*)"/U', $string, $result);
$foo = array_pop($result);

回答by CONvid19

$imgTag = <<< LOB
<img border="0" src="/images/image.jpg" alt="Image" width="100" height="100" />
<img border="0" src="/images/not_match_image.jpg" alt="Image" width="100" height="100" />
LOB;

preg_match('%<img.*?src=["\'](.*?)["\'].*?/>%i', $imgTag, $matches);
$imgSrc = $matches[1];

DEMO

DEMO



NOTE:You should use an HTML Parser like DOMDocumentand NOTa regex.

注意:您应该使用 HTML 解析器,DOMDocument不是正则表达式。

回答by squarephoenix

$str = '<img border="0" src=\'/images/image.jpg\' alt="Image" width="100" height="100"/>';

preg_match('/(src=["\'](.*?)["\'])/', $str, $match);  //find src="X" or src='X'
$split = preg_split('/["\']/', $match[0]); // split by quotes

$src = $split[1]; // X between quotes

echo $src;

Other regexp's can be used to determine if the pulled src tag is a picture like so:

其他正则表达式可用于确定拉取的 src 标签是否是这样的图片:

if(preg_match('/([jpg]{3}$)|([gif]{3}$)|([jpeg]{3}$)|([bmp]{3}$)|([png]{3}$)/', $src) == 1) {
//its an image
}

回答by Jitendra

There could be two easy solutions:

可能有两个简单的解决方案:

  1. HTML it self is an xml so you can use any XML parsing method if u load the tag as XML and get its attribute tottally dynamically even dom data attribute (like data-time or anything).....
  2. Use any html parser for php like http://mbe.ro/2009/06/21/php-html-to-array-working-one/or php parse html to array Google this
  1. HTML本身就是一个xml,因此如果您将标签加载为XML并完全动态地获取其属性甚至dom数据属性(如数据时间或任何东西),则您可以使用任何XML解析方法......
  2. 对 php 使用任何 html 解析器,如 http://mbe.ro/2009/06/21/php-html-to-array-working-one/或 php 解析 html 来排列谷歌这个