Get all images and return the src [duplicate]

六月ゝ 毕业季﹏ 提交于 2019-12-13 08:38:35

问题


My code bellow grabs some content. in this content there are some photos. How can i loop through this content find all images and return their src?

my code so far:

$items = $html->find('div.post-single-content',0)->children(1)->outertext;
foreach($items $node) { 
$node->find('img');
}
print_r ($node);

回答1:


Don't use regex, use a parser. Example:

$string = '<img src="You want this" style="width:200px;" />';
$doc = new DOMDocument();
$doc->loadHTML($string);
$images = $doc->getElementsByTagName('img');
foreach ($images as $image) {
     echo $image->getAttribute('src') . "\n";
}

Output:

You want this



来源:https://stackoverflow.com/questions/32928184/get-all-images-and-return-the-src

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!