Jump to content

Tiny Problem


Jarfilli

Recommended Posts

Hello guys,

I am having a little trouble with a regex. I am trying to match megaupload links eg. http://www.megaupload.com/?d=N241NIPD

 

it's always 'http://www.megaupload.com/?d=' followed by 8 alphanumeric characters.

 

the regex I have is

 

preg_match('/http\:\/\/www\.megaupload\.com\/(?:|..\/)\?d=.{8}(\/|)/',$url,$matches);I have been at it for hours and for the life of me can't figure out why it's not working. If anyone would like to tell me how stupid I am being I would appreciate it.

 

Thanks in advance

 

Jarfilli

Link to comment
Share on other sites

There are a couple of prefab php functions for parsing url strings....you can do something like this:

 

function getQueryParam($url,$param) {
  $query = parse_url((string)$url);
  parse_str($query['query'],$query);
  return (isset($query[$param])) ? $query[$param] : false;
}

$url = "http://www.megaupload.com/?d=N241NIPD";
$d = getQueryParam($url,'d');
echo $d;

 

But if you want to do it with regex (based on that url given, no other "rules" for what it could be...):

 

$url = "http://www.megaupload.com/?d=N241NIPD";
preg_match('~http://www\.megaupload\.com/\?d=([a-z0-9]{8})~i',$url,$d);
echo $d[1];

Link to comment
Share on other sites

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.