Jump to content

[SOLVED] Finding a url and storing in a variable


edking1

Recommended Posts

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

 

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.

 

 

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.