...

View Full Version : Extract URL From A String



Andy Booth
06-08-2007, 11:22 AM
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

mlseim
06-08-2007, 01:19 PM
Would you rather search the directory for .jpg images and create
a list of them, or search the HTML text? If you know the directory,
you can find all .jpg files that it contains.

Andy Booth
06-08-2007, 01:39 PM
I needed to search the string which was HTML formatted text.

I managed it in the end...


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

although i'm convinced there is a much cleaner approach to doing it...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum