edking1 Posted November 10, 2009 Share Posted November 10, 2009 Hi everyone This is probably quite simple but I've spent hours tearing my hair out trying to get something to work.. Say I have the following : $text = "blah blah blah <img src = \"someUrl.com\" /> blah blah <img src = \"anotherUrl.com\" /> blah"; How would I go about taking each of the src Urls and placing them into an array? I'd massively appreciate any help! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/181013-solved-finding-a-url-and-storing-in-a-variable/ Share on other sites More sharing options...
cags Posted November 10, 2009 Share Posted November 10, 2009 There are dozens of patterns about for simply matching urls, which can be found easily. If you only wish to match url's that are in the src"" attribute of an image tag then you could use something like... $input = '"blah blah blah <img src="someUrl.com" /> blah blah <img src="anotherUrl.com" /> blah"'; preg_match_all('~<img.*?src=("|\')([^\1]+?)\1~is', $input, $out); echo '<pre>'; print_r($out); echo '</pre>'; Note: I removed the spaces around the equal signs and the escape characters in my example. If they are actually there in the source you will need to include them in the pattern. Quote Link to comment https://forums.phpfreaks.com/topic/181013-solved-finding-a-url-and-storing-in-a-variable/#findComment-955014 Share on other sites More sharing options...
edking1 Posted November 10, 2009 Author Share Posted November 10, 2009 That worked perfectly! Thank you SO much, you've really helped me out here. Quote Link to comment https://forums.phpfreaks.com/topic/181013-solved-finding-a-url-and-storing-in-a-variable/#findComment-955049 Share on other sites More sharing options...
nrg_alpha Posted November 10, 2009 Share Posted November 10, 2009 edking1, once your topic has been resolved, you can flag it as 'solved'. Quote Link to comment https://forums.phpfreaks.com/topic/181013-solved-finding-a-url-and-storing-in-a-variable/#findComment-955058 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.