SharkBait Posted June 17, 2008 Share Posted June 17, 2008 Hi, What I am trying to figure out is how to retrieve the image tag from content within my website. I looked at this post: http://www.phpfreaks.com/forums/index.php/topic,139613.0.html but it does not work when there are sentaces etc between the <img> tags. What I am trying to accomplish is pulling the first image in a paragraph and then reusing it else where. Ex: This is a post and within this post we have a <img src="image.jpg"> in which we need to locate and use else wehre but then if we have <img src="another.jpg"> image laying around the above function (in the link provided) does not seem to function correctly. How do I go about matching content (assume its in variable $body) for the first <img> tag so that I can pull the src="" value from it and use it else where? I thought about trying <?php preg_match('/<img[\s]+src/=(.*)/>/', $body, $results); ?> But that didn't really do the trick as the 2nd element in the $results array is a combination of the string and the 2nd <img> tag. Link to comment https://forums.phpfreaks.com/topic/110640-find-images-within-site-content/ Share on other sites More sharing options...
effigy Posted June 17, 2008 Share Posted June 17, 2008 That pattern gave me an error. How about this? <pre> <?php $body = <<<BODY This is a post and within this post we have a <img src="image.jpg"> in which we need to locate and use else wehre but then if we have <img src="another.jpg"> image laying around the above function (in the link provided) does not seem to function correctly. BODY; preg_match('/<img[^>]+src="(.*?)"[^>]*>/', $body, $results); print_r($results); ?> </pre> Link to comment https://forums.phpfreaks.com/topic/110640-find-images-within-site-content/#findComment-567607 Share on other sites More sharing options...
SharkBait Posted June 17, 2008 Author Share Posted June 17, 2008 Ah that works nicely. and $results[1] is the filename cool. Thank you Link to comment https://forums.phpfreaks.com/topic/110640-find-images-within-site-content/#findComment-567608 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.