Andy Booth Posted June 8, 2007 Share Posted June 8, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/54724-solved-extract-url-from-string/ Share on other sites More sharing options...
Andy Booth Posted June 8, 2007 Author Share Posted June 8, 2007 Can't edit for some reason? Well my post didn't display correctly...should of shown... blah blah blha <img src="http://www.mysite.com/image.jpg" /> blah blah blah Quote Link to comment https://forums.phpfreaks.com/topic/54724-solved-extract-url-from-string/#findComment-270662 Share on other sites More sharing options...
brissy_matty Posted June 8, 2007 Share Posted June 8, 2007 <? $string='blah blah blha <img src="http://www.mysite.com/image.jpg" /> blah blah blah'; $imageurl=explode('"',$string); echo $imageurl[1]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/54724-solved-extract-url-from-string/#findComment-270664 Share on other sites More sharing options...
Andy Booth Posted June 8, 2007 Author Share Posted June 8, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/54724-solved-extract-url-from-string/#findComment-270669 Share on other sites More sharing options...
brissy_matty Posted June 8, 2007 Share Posted June 8, 2007 how about - <? $string='blah blah blha <img src="http://www.mysite.com/image.jpg" /> blah blah blah'; $imageurl=explode('src="http://',$string); $string=explode('"',$string); echo $string[1]; ?> There most likely is a much better way ? Quote Link to comment https://forums.phpfreaks.com/topic/54724-solved-extract-url-from-string/#findComment-270673 Share on other sites More sharing options...
Andy Booth Posted June 8, 2007 Author Share Posted June 8, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/54724-solved-extract-url-from-string/#findComment-270710 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.