Php 使用正则表达式从url获取id

Php 使用正则表达式从url获取id,php,regex,preg-match-all,Php,Regex,Preg Match All,URL始终以以下内容开头: 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%2F12345

URL始终以以下内容开头:

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
ID始终为数字,但位数可能会有所不同

如何从上述示例URL获取id(
1234567
123456

我尝试过使用以下模式,但运气不佳(它不会返回任何匹配项):


也有一种不用解析的方法。假设$url=url


也有一种不用解析的方法。假设$url=url


我建议您首先提取
url
querystring参数并对其进行url解码:

$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);
像这样:

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, " "));
}

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

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

$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);
var url = 'http://example.com/movie.swf?url=http%3A%2F%2Fexample.com%2Fmovie%2F1234567';
var p = getParameterByName(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, " "));
}
然后使用一些正则表达式解析
p
,并提取必要的信息,如
/\d+/

var url = 'http://example.com/movie.swf?url=http%3A%2F%2Fexample.com%2Fmovie%2F1234567';
var p = getParameterByName(url, 'url');
接吻。我本来打算使用
parse_url()
,但是如果没有正则表达式,就无法解析查询字符串


接吻。我本来打算使用
parse_url()
,但是如果没有正则表达式,就无法解析查询字符串。

看起来您需要转义一些特殊字符。 尝试:


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

看起来您需要转义一些特殊字符。 尝试:


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

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

$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]);
   }
}

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

$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]);
   }
}