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

发布于 2024-12-10 09:12:36 字数 863 浏览 1 评论 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

URL 始终以以下形式开头:

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

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

如何从上面的示例 URL 获取 id(1234567123456)?

我尝试使用以下模式但没有成功(它不返回任何匹配项):

/^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%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)$/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一袭白衣梦中忆 2024-12-17 09:12:36

我建议您首先解析此网址并提取网址 查询字符串参数并对其进行 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+/

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+/.

不语却知心 2024-12-17 09:12:36

使用正确的 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'];
    }
}

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'];
    }
}
a√萤火虫的光℡ 2024-12-17 09:12:36
$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]);
   }
}

吻。我原本打算使用 parse_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]);
   }
}

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

萌逼全场 2024-12-17 09:12:36

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

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);
伴随着你 2024-12-17 09:12:36

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

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

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+)$/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文