Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by whitelisting our website.

How to find src attribute of image tag in php?

In my case, I had a string containing exactly one <img /> tag (and no other markup). And I was trying to get the src attribute.

// get the featured image
$image = '<img src="path/to/image_file" />';

// get the src for that image
$pattern = '/src="([^"]*)"/';
preg_match($pattern, $image, $matches);
$src = $matches[1];

In the pattern to grab the title and the alt, you could simply use $pattern = ‘/title=”([^”]*)”/’; to grab the title and $pattern = ‘/alt=”([^”]*)”/’; to grab the alt.

I hope this solution work for you also. Share and Enjoy. And inspire me by your comments 😉

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Wordpress Social Share Plugin powered by Ultimatelysocial