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. Quote Link to comment 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> Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.