View Full Version : Extract URL From A String

Andy Booth
06-08-2007, 12:22 PM

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



06-08-2007, 02: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, 02:39 PM
I needed to search the string which was HTML formatted text.

I managed it in the end...

if ((substr($string[1], -4, 4) == ".jpg"))
echo $string[1];

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