Jump to content

Find Images within Site Content


SharkBait

Recommended Posts

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

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.