开发者

Extract src from image Tag

开发者 https://www.devze.com 2023-03-02 05:31 出处:网络
I have an image Tag like <img src=\"abc\" height=\"20\" width=\"50\" /> How can I extract 开发者_开发问答only the source from it in php?You could use SimpleXML very quickly here:

I have an image Tag like

<img src="abc" height="20" width="50" />

How can I extract 开发者_开发问答only the source from it in php?


You could use SimpleXML very quickly here:

$sxe = new SimpleXMLElement('<img src="abc" height="20" width="50" />');
$src = (string) $sxe['src'];


The best way would be to use regular expressions.

<?php
$tag='<img src="abc" height="20" width="50" />';
$extracted=preg_replace('/<img [^>]*src=[\'"]([^\'"]+)[\'"][^>]*>/','\\1',$tag);
echo $extracted;
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消