Jump to content

[SOLVED] Extract URL from String


Andy Booth

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! :)

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.