Extract src from image Tag
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;
?>
精彩评论