简体   繁体   English

PHP preg_match url的一部分

[英]PHP preg_match part of url

I am trying to create a url router in PHP, that works like django's. 我试图用PHP创建一个url路由器,就像django一样。

The problems is, I don't know php regular expressions very well. 问题是,我不太清楚php正则表达式。

I would like to be able to match urls like this: 我希望能够匹配这样的网址:

/post/5/
/article/slug-goes-here/

I've got an array of regexes: 我有一系列正则表达式:

$urls = array(      
  "(^[/]$)" => "home.index",     
  "/post/(?P<post_id>\d+)/" => "home.post",    
);

The first regex in the array works to match the home page at / but I can't get the second one to work. 数组中的第一个正则表达式用于匹配/的主页,但我不能让第二个正则表达式工作。

Here's the code I am using to match them: 这是我用来匹配它们的代码:

foreach($urls as $regex => $mapper) {
    if (preg_match($regex, $uri, $matches)) {
    ...
    }
}

I should also note that in the example above, I am trying to match the post_id in the url: /post/5/ so that I can pass the 5 along to my method. 我还应该注意,在上面的例子中,我试图匹配url: /post/5/中的post_id ,以便我可以将5传递给我的方法。

You must delimit the regex. 你必须划定正则表达式。 Delimiting allows you to provide 'options' (such as 'i' for case insensitive matching) as part of the pattern: 分隔允许您提供“选项”(例如“i”用于不区分大小写的匹配)作为模式的一部分:

,/post/(?P<post_id>\d+)/,

here, I have delimited the regex with commas. 在这里,我用逗号分隔了正则表达式。

As you have posted it, your regex was being delimited with /, which means it was treating everything after the second / as 'options', and only trying to match the "post" part. 正如你发布的那样,你的正则表达式用/分隔,这意味着它在第二个/作为'选项'之后处理所有内容,并且只尝试匹配“post”部分。

The example you are trying to match against looks like it isn't what you're actually after based on your current regex. 根据您当前的正则表达式,您尝试匹配的示例看起来并不是您实际执行的操作。

If you are after a regex which will match something like; 如果你正在使用匹配类似的正则表达式;

/post/P1234/

Then, the following: 然后,以下内容:

preg_match(',/post/(P\d+)/,', '/post/P1234/', $matches);
print_r($matches);

will result in: 将导致:

Array
(
    [0] => /post/P1234/
    [1] => P1234
)

Hopefully that clears it up for you :) 希望能为你清除它:)

Edit 编辑

Based on the comment to your OP, you are only trying to match a number after the /post/ part of the URL, so this slightly simplified version: 根据您对OP的评论,您只是尝试匹配URL的/ post /部分后面的数字,因此这个稍微简化的版本:

preg_match(',/post/(\d+)/,', '/post/1234/', $matches);
print_r($matches);

will result in: 将导致:

Array
(
    [0] => /post/1234/
    [1] => 1234
)

If your second RegExp is meant to match urls like /article/slug-goes-here/, then the correct regular expression is 如果您的第二个RegExp旨在匹配像/ article / slug-goes-here /这样的网址,那么正确的正则表达式是

#\/article\/[\w-]+\/#

That should do it! 应该这样做! Im not pretty sure about having to escape the /, so you can try without escaping them. 我不太确定必须逃避/,所以你可以尝试不逃避它们。 The tag Im guessing is extracted from a .NET example, because that framework uses such tags to name matching groups. 标记Im guessing是从.NET示例中提取的,因为该框架使用此类标记来命名匹配组。

I hope I can be of help! 我希望我可以帮忙!

php 5.2.2: Named subpatterns now accept the syntax (?<name>) and (?'name') as well as (?P<name>) . php 5.2.2:命名子模式现在接受语法(?<name>)(?'name')以及(?P<name>) Previous versions accepted only (?P<name>) . 以前的版本只接受(?P<name>)

http://php.net/manual/fr/function.preg-match.php http://php.net/manual/fr/function.preg-match.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM