I am using this PHP function to grab all <img> tags within any given HTML.
function extract_images($content)
{
$img = strip_tags(html_entity_decode($content),'<img>');
$regex = '~src="[^"]*"~';
preg_match_all($regex, $img, $all_images);
return $all_images;
}
This works and returns all images (gif, png, jpg, etc).
Anyone know how to change the regex…
~src="[^"]*"~
in order to only get files with JPG or JPEG extension?
Thanks a bunch.
Sooner or later the Regex Enforcement Agency will show up. It might as well be me 🙂
The proper way to do this is with a proper HTML DOM parser. Here’s a
DOMDocumentsolution. The usefulness of this is in that it’s more robust than parsing the HTML by regex, and also gives you the ability to access or modify other HTML attributes on your<img>nodes at the same time.See other answers for the simple regex solution, or adapt from the regex inside my foreach loop.