[?] How To: preg_match an image URL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jeff Gordon
Forum Newbie
Posts: 2
Joined: Fri Oct 31, 2008 9:11 pm

[?] How To: preg_match an image URL

Post by Jeff Gordon »

I am a complete idiot when it comes to regex.
I was wondering if someone could help me preg_match the image in the code below?

Code: Select all

<embed src="http://domain.tld/player.swf" flashvars="file=http://api.ning.com/files/pXDMMC6clGoLXXXXXX_897UyRigd-8oOTmz2e4q*VXtH7BdpxV2UijYWHQeIr-dFc1grgsnmYllBc4e0kon3CjBvTE3BmOa/tmp42620.flv&image=http://imageshack.us/image_i_need.png&logo=http://domain.tld/logo.png&stretching=fill&abouttext=domain.tld&aboutlink=http://domain.tld&title=Video Title&type=flv" width="520" height="372" align="middle" allowFullScreen="true" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"/>
the code I have now is:

Code: Select all

if(preg_match("/http:\/\/api.ning.com\/files/", $embed_code))
    {
    if (preg_match("image=([\w\-]+))", $embed_code, $matches))
    {
    $img = $matches[1];
    }
    }
this code only matches

Code: Select all

http
I need the entire URL after:

Code: Select all

&image
the match should stop after the image extension or before this:

Code: Select all

&logo
thank you for any help.
also, please direct me to any regex sites.
thanks.

-Jeff
Jeff Gordon
Forum Newbie
Posts: 2
Joined: Fri Oct 31, 2008 9:11 pm

Re: [?] How To: preg_match an image URL

Post by Jeff Gordon »

L O L,

well I actually answered my own question.
this is the code:

Code: Select all

if(preg_match("/http:\/\/api.ning.com\/files/", $embed_code))
    {
    if (preg_match("!&image=(.+)&logo=!", $embed_code, $matches))
    {
    $img = $matches[1];
    }
    }
if someone has something better please let me know.
also, please direct me to any regex / preg_match sites.

thank you.

-Jeff
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: [?] How To: preg_match an image URL

Post by requinix »

Something more like

Code: Select all

/image=([^&"]+)/
might be smarter.
Post Reply