Laura
Laura

Reputation: 79

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?

Upvotes: 0

Views: 251

Answers (2)

Stephen
Stephen

Reputation: 18917

You could use SimpleXML very quickly here:

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

Upvotes: 2

null
null

Reputation: 11839

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;
?>

Upvotes: -1

Related Questions