[英]Regex to match uri after mod_rewrite
我正在寻找一个PHP
PCRE
正则表达式来匹配用Apache
的mod_rewrite
模块重写的uri。 uri如下:
/param1/param2/param3/param4
uri的规则
/
; -
和_
; /\/[a-zA-Z0-9_\-\/]+$/
我假设它必须以/
开头,这样的东西不应该匹配/param1/param2/param3/param4*
怎么样:
if (preg_match("~^(?:/[\w-]+)+/?$~", $string)) {
# do stuff
}
说明:
The regular expression:
(?-imsx:^(?:/[\w-]+)+/?$)
matches as follows:
NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
^ the beginning of the string
----------------------------------------------------------------------
(?: group, but do not capture (1 or more times
(matching the most amount possible)):
----------------------------------------------------------------------
/ '/'
----------------------------------------------------------------------
[\w-]+ any character of: word characters (a-z,
A-Z, 0-9, _), '-' (1 or more times
(matching the most amount possible))
----------------------------------------------------------------------
)+ end of grouping
----------------------------------------------------------------------
/? '/' (optional (matching the most amount
possible))
----------------------------------------------------------------------
$ before an optional \n, and the end of the
string
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.