Extract image source from img tag

David Carr

1 min read - 28th Jan, 2016

To extract the image source from this:

<p><img alt="" src="/images/logo.jpg" style="width: 300px; height: 97px;" /></p>

Use preg_match to extract the image src:

$img = stripslashes($source);
$str = preg_replace('#<p>.+?src=[\'"]([^\'"]+)[\'"].+</p>#i', "$1", $img);
$source = trim($str);
0 comments
Add a comment

Copyright © 2006 - 2024 DC Blog - All rights reserved.