PHP get image src

别说谁变了你拦得住时间么 提交于 2019-12-13 03:33:40

问题


$variable = '<img src="http://www.gravatar.com/avatar/66ce1f26a725b2e063d128457c20eda1?s=32&d=identicon&r=PG" height="32" width="32" alt=""/>';

How to get src of this image?

Like:

$src = 'http://www.gravatar.com/avatar/66ce1f26a725b2e063d128457c20eda1?s=32&d=identicon&r=PG';

Thanks.


回答1:


You can use an html parser for this. See this one: http://simplehtmldom.sourceforge.net/ (I hope this works for nodes, not just for entire pages).




回答2:


use regex

preg_match( '@src="([^"]+)"@' , $variable , $match );

And src will be in $match, see print_r( $match ). But this is only for this situation, if you want universal solution (like ' against " etc.) use html/DOM parser.




回答3:


Use HTML DOM. Download and include simple_html_dom.php file in your script.

Then get image URLs like this:

$html = str_get_html('<img src="http://www.gravatar.com/avatar/66ce1f26a725b2e063d128457c20eda1?s=32&d=identicon&r=PG" height="32" width="32" alt=""/>');

// Find all images
foreach($html->find('img') as $element)
       echo $element->src . '<br>';


来源:https://stackoverflow.com/questions/3510535/php-get-image-src

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