I want to filter url's like bartender/开发者_如何学Pythonprofili/20/friends using preg_match.
Regex:
/^\/bartender\/profili\/d+\/friends/
Unfortunately it doesn't work.
Could somebody help?
Do you want to capture the \d+? If so, enclose them in ( ), that should fix it. Also, if you're regex uses /, I'd suggest using a different delimiter, less escaping that way:
<?php
preg_match(
'~^/bartender/profili/(\d+)/friends~',
'/bartender/profili/20/friends',
$matches
);
var_dump( $matches );
Addendum: and - like everyone already mentioned - you indeed forgot the \ in front of the d+, but that was easy to overlook if you have to escape every / ;)
You have a slash in front of d+
(and have selected a badly readable delimiter for this situation). Try this:
#^\/bartender\/profili\/\d+\/friends#
you forgot to escape \d+
/^\/bartender\/profili\/\d+\/friends/
You are missing a backslash before d+
: \d+
"bartender\/profili\/[0-9]+\/friends
" also works. Here's an excellent resource for trying regular expressions: http://www.regextester.com/
You do not have to escape forward slashes, but you need to escape meta-characters (like 'd' for decimal digits)
^/bartender/profili/\d+/friends/
精彩评论