How to put all the images in a block of text into an array, PHP?
Question
function has_thumbnail_image(&$post) {
$content = $post->post_content;
return preg_match('/<img[^>]+src="(.*?)"[^>]*>/', $content, $results);
}
I need a function that goes through a block of dynamically returned text and puts all of the images contained within into an array (or more specifically the image source of each image). The function above only gives me the first image and I cannot work out how to make this loop keep happening until all of the images are in the array. Any help on this would be much appreciated. Thanks
Solution
You may want to investigate preg_match_all
. If I recall correctly, preg_match
only searches for the first match and then stops.
OTHER TIPS
You are very close! You just need preg_match_all
instead of preg_match
.
I don´t know how well you know your source, but you might want to allow single quotes for the src attribute.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow