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
https://forums.phpfreaks.com/topic/213524-tiny-problem/
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
https://forums.phpfreaks.com/topic/213524-tiny-problem/#findComment-1111495
Share on other sites

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.