0
http://example.com/movie.swf?url=http%3A%2F%2Fexample.com%2Fmovie%2F1234567
http://example.com/movie.swf?url=http%3A%2F%2Fexample.com%2Fmovie%2F1234567%2Fsubtitle
http://example.com/movie.swf?url=http%3A%2F%2Fexample.com%2Fmovie%2F123456
http://example.com/movie.swf?url=http%3A%2F%2Fexample.com%2Fmovie%2F123456%2Fsubtitle
http://example.com/movie.swf?url=http%3A%2F%2Fexample.com%2Fmovie%2F1234567%2F
http://example.com/movie.swf?url=http%3A%2F%2Fexample.com%2Fmovie%2F123456%2F

The URL's always start with:

http://example.com/movie.swf?url=http%3A%2F%2Fexample.com%2Fmovie%2F

The ids are always numeric, however the number of digits can vary.

How to get the id (1234567 and 123456) from above sample URL's?

I've tried using the following pattern without luck (it doesn't return any matches):

/^http://example.com/movie.swf?url=http%3A%2F%2Fexample.com%2Fmovie%2F(\d)$/
PeeHaa
  • 71,436
  • 58
  • 190
  • 262

5 Answers5

3

I would recommend you to first parse this url and extract the url query string parameter and url decoding it:

function getParameterByName(url, name)
{
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(url);
  if(results == null)
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

like this:

var url = 'http://example.com/movie.swf?url=http%3A%2F%2Fexample.com%2Fmovie%2F1234567';
var p = getParameterByName(url, 'url');

and then use some regex to parse p and extract the necessary information like /\d+/.

Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
1

There's a way without parsing too. Assuming $url = URL

http://codepad.org/t91DK9H2

$url = "http://example.com/movie.swf?url=http%3A%2F%2Fexample.com%2Fmovie%2F1234567%2Fsubtitle";
$reg = "/^([\w\d\.:]+).*movie%2F(\d+).*/";
$id = preg_replace($reg,"$2",$url);
Abhinav Pandey
  • 499
  • 5
  • 16
1
$urls = array(
   'http://example.com/movie.swf?url=http%3A%2F%2Fexample.com%2Fmovie%2F1234567'
   , 'http://example.com/movie.swf?url=http%3A%2F%2Fexample.com%2Fmovie%2F1234567%2Fsubtitle'
   , 'http://example.com/movie.swf?url=http%3A%2F%2Fexample.com%2Fmovie%2F123456'
   , 'http://example.com/movie.swf?url=http%3A%2F%2Fexample.com%2Fmovie%2F123456%2Fsubtitle'
   , 'http://example.com/movie.swf?url=http%3A%2F%2Fexample.com%2Fmovie%2F1234567%2F'
   , 'http://example.com/movie.swf?url=http%3A%2F%2Fexample.com%2Fmovie%2F123456%2F'
);

foreach ($urls as $url) {
   if (preg_match('/%2Fmovie%2F(\d+)/', $url, $matches)) {
      var_dump($matches[1]);
   }
}

KISS. I was originally going to use parse_url(), but there is no way to parse a query string without regular expressions anyway.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
1

With proper URL parsing functions you can do this:

parse_str(parse_url($url, PHP_URL_QUERY), $params);
if (isset($params['url'])) {
    parse_str(parse_url($params['url'], PHP_URL_QUERY), $params);
    if (isset($params['movie'])) {
        $movie = $params['movie'];
    }
}
Gumbo
  • 643,351
  • 109
  • 780
  • 844
0

It looks likes you need to escape some special characters. try:

/^http://example.com/movie.swf\?url=http%3A%2F%2Fexample.com%2Fmovie%2F(\d+)$/

aaray
  • 41
  • 2