Extract image source from img tag

David Carr

Tutorials PHP & MySQL

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);
Laravel Modules Your Logo Your Logo Your Logo

Become a sponsor

Help support the blog so that I can continue creating new content!

Sponsor

My Latest Book

Modular Laravel Book - Laravel: The Modular way

Learn how to build modular applications with Laravel Find out more

Subscribe to my newsletter

Subscribe and get my books and product announcements.

Fathom Analytics $10 discount on your first invoice using this link

© 2006 - 2024 DC Blog. All code MIT license. All rights reserved.