Jump to content

[SOLVED] Finding a url and storing in a variable


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.

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.