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
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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.