正则表达式PHP-从img标签中隔离src属性 [英] Regex & PHP - isolate src attribute from img tag
本文介绍了正则表达式PHP-从img标签中隔离src属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用PHP,如何隔离$ foo的src属性的内容?我要寻找的最终结果只会是" http://example.com/img/image. jpg "
With PHP, how can I isolate the contents of the src attribute from $foo? The end result I'm looking for would give me just "http://example.com/img/image.jpg"
$foo = '<img class="foo bar test" title="test image" src="http://example.com/img/image.jpg" alt="test image" width="100" height="100" />';
推荐答案
如果您不希望使用正则表达式(或任何非标准的PHP组件),请使用内置的
If you don't wish to use regex (or any non-standard PHP components), a reasonable solution using the built-in DOMDocument class would be as follows:
<?php
$doc = new DOMDocument();
$doc->loadHTML('<img src="http://example.com/img/image.jpg" ... />');
$imageTags = $doc->getElementsByTagName('img');
foreach($imageTags as $tag) {
echo $tag->getAttribute('src');
}
?>
这篇关于正则表达式PHP-从img标签中隔离src属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文