Jump to content

Recommended Posts

Hi

 

I've spent ALL morning trying to get this to work, hope someone on here can help me!

 

Ok, I have a string retrieved from a database that contains some HTML-formatted text. What I want to do is search through this text for all images and extract the URL's from them. So if the string consisted of...

 

blah blha blah <img src="http://www.mysite.com/image.jpg" /> blah blah blah

 

Then I want to have the value http://www.mysite.com/image.jpg in a variable. I see what I have to do...search for all occurences of words that start with src=" and end with .jpg" and get the value out of the middle, but just can't suss out the way to make it work! Please help! :)

 

Thanks

 

AB

Link to comment
https://forums.phpfreaks.com/topic/54724-solved-extract-url-from-string/
Share on other sites

Thanks

 

That works for that string only though, I want it to work for any given string, find the first image. It's actually going through a review of an item, so the string is fairly large. Also some of the images might have a class="" and an alt="" before the src="" which breaks the code.

 

Can anyone build on this please? :)

Thanks, I ended up modifying that a little bit to make it work, came out with...

 

$imageurl=explode('src=',$row[2]);
			$string=explode('"',$imageurl[1]);
			if ((substr($string[1], -4, 4) == ".jpg"))
			{
				echo $string[1];
			}

 

It checks to make sure that the src= is refering to a jpg, not anything else (I have embeded videos and such as well which did confuse it).

 

Thanks for all your help! :)

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.