使用正则表达式从URL获取id

时间:2011-10-15 14:54:48

标签: php regex preg-match-all

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

网址始终以:

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

ID始终为数字,但数字位数可能会有所不同。

如何从上面的示例网址获取ID(1234567123456)?

我尝试使用以下模式没有运气(它没有返回任何匹配):

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

5 个答案:

答案 0 :(得分:3)

我建议您先parse this url并提取url查询字符串参数并对其进行解码:

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, " "));
}
像这样:

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

然后使用一些正则表达式来解析p并提取必要的信息,例如/\d+/

答案 1 :(得分:1)

有一种方法没有解析。假设$ 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);

答案 2 :(得分: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。我原本打算使用parse_url(),但无论如何都无法解析没有正则表达式的查询字符串。

答案 3 :(得分:1)

使用正确的URL解析功能,您可以执行以下操作:

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'];
    }
}

答案 4 :(得分:0)

看起来你需要逃避一些特殊角色。 尝试:

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