mod_rewrite 的正则表达式仅采用之前的内容 --
嗨,我有一个网址,读起来就像
/users/nick--last-first-middle-bla-bla-bla.html
我需要的是使正则表达式接受尼克,即使它像这样
ni-ck--last-first-middle-bla-bla-bla.html
- bla-bla可能是任意数量的变量,我只需要获取“nick”部分,无论它后面是什么,因为它总是分开的经过 ” - ”。
- 昵称最多 50 个字符
有人可以建议使用正则表达式进行 mod 重写吗,谢谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
选项 1:
如果 URL 始终具有昵称、最后一个、第一个和中间,后跟可选变量,并且昵称必须为 1 到 50 个字符,则此测试规则将起作用。
给定:
/users/ni-ck--last-first-middle-bla-bla-bla.html
此规则分离并提供以下$_GET
数组变量:选项 2:
如果 URL 始终具有 1 到 50 个字符的昵称,后跟
"--"
并且后面的所有内容都是可选的,则此测试规则将起作用。 (这个没有分离出名称部分):给定:
/users/ni-ck--last-first-middle-bla-bla-bla.html
此规则分离并提供以下内容$_GET
数组变量:Option 1:
If the URL will always have a nick, last, first and middle, followed by optional vars and the nick must be from 1 to 50 chars, then this tested rule will work.
Given:
/users/ni-ck--last-first-middle-bla-bla-bla.html
this rule separates out and provides the following$_GET
array variables:Option 2:
If the URL will always have a nick from 1 to 50 chars, followed by
"--"
and everything following that is optional, then this tested rule will work. (This one does not separate out the name parts):Given:
/users/ni-ck--last-first-middle-bla-bla-bla.html
this rule separates out and provides the following$_GET
array variables:应该用 1 美元捕获你的昵称。 (不记得非捕获括号在 mod-Rewrite 中是否有效,否则只是丢失
?:
)Should capture your nick in $1. (Don't remember if the non capturing parenthesis work in mod-Rewrite, or else just loose the
?:
)^/users/(.{0,50}).*(?=--)(?
^/users/
:匹配起始字符串(.{0,50})
:将任意字符(最多 50 个)匹配到该组中.*
:匹配第50个之后的任意字符但仅匹配到
--
(不包括在内):(?=--)
:正向先行,--
紧随此位置之后(?
请注意,
--
是必需的,否则模式将不匹配。^/users/(.{0,50}).*(?=--)(?<!--.*)
matches the "nick" part up to 50 characters or up to--
(whichever is shortest) in a group, using quantifiers and lookarounds for the constraints.^/users/
: match the start string(.{0,50})
: match any character, up to 50, into this group.*
: match any character after the 50thbut only match until
--
(not inclusive):(?=--)
: positive lookahead,--
exists immediately after this location(?<!--.*)
: negative lookbehind,--
does not exist anywhere before this locationNote that the
--
is required or the pattern will not match.^/users/(?(.{0,50}?--)(.*?)|(.{50}).+)--
不适用于环视,而是使用惰性量词和条件(我不知道 mod_rewrite 是否支持)将短昵称放入组 1 或将截止昵称放入组 2,因此您可以连接这些组以获得请求的昵称。^/users/
:匹配起始字符串(?
:条件表达式的开始(.{0,50}?--)
:[if] 检查这里是否有短昵称(第一个-- 之前不超过 50 个字符)
)(.*?)
:[then] 将任何字符匹配到该组中(直到第一个--
,见下文,由于延迟匹配)|
:条件表达式中的分隔符(.{50}).+
:[else] 仅匹配该组中的前 50 个字符)
:条件表达式结束--
:最终匹配实际上并不清楚如何处理太长的缺口。如果长缺口可以被忽略而不是被切断,那么也许以下就足够了?
^/users/(.{0,50}?)--
仅匹配第一组中的短昵称(惰性量词)...^/users/(?(.{0,50}?--)(.*?)|(.{50}).+)--
does not work with lookarounds but instead uses lazy quantifiers and a conditional (I do not know if either is supported by mod_rewrite) which puts either the short nick in group 1 or the cut off nick in group 2, so you can just concatenate those groups to get the requested nick.^/users/
: match the start string(?
: start of the conditional expression(.{0,50}?--)
: [if] check if we have a short nick here (no more than 50 characters before first--
)(.*?)
: [then] match any character into this group (until first--
, see below, due to lazy matching)|
: delimiter in the conditional expression(.{50}).+
: [else] only match first 50 characters into this group)
: end of the conditional expression--
: final matchActually it is not really clear what to do with nicks that are too long. If long nicks can just be ignored instead of cut off, then maybe the following suffices?
^/users/(.{0,50}?)--
only matches short nicks in the first group (lazy quantifier)…尝试在 .htaccess 文件中遵循以下规则:
测试
如果我有这样的 foo.php:
For the URL of
/users/nicked-ic-ic-ack--last-first-middle-bla-bla -bla.html
它打印:string(21) "name=nicked-ic-ic-ack"
对于
/users/ni-ck--last-first-middle- 的 URL bla-bla-bla.html
它打印:string(10) "name=ni-ck"
对于
/users/nick--last-first-middle-bla-bla-bla.html
的 URL,它打印:string(9) "name=nick"
对于 URL
/users/nick--last--first
(多个--
)它打印:string(9) "name=nick"
对于
/users/nicked-ic-ic-nicked-nicked-nicked-nicked-nicked-nicked-nicked- 的 URL ack--last-first-middle-bla-bla-bla.html
它抛出404,因为名称长度现在>; 50404 - 未找到
Try following rule in your .htaccess file:
TESTING
If I have foo.php like this:
For the URL of
/users/nicked-ic-ic-ack--last-first-middle-bla-bla-bla.html
it prints:string(21) "name=nicked-ic-ic-ack"
For the URL of
/users/ni-ck--last-first-middle-bla-bla-bla.html
it prints:string(10) "name=ni-ck"
For the URL of
/users/nick--last-first-middle-bla-bla-bla.html
it prints:string(9) "name=nick"
For the URL
/users/nick--last--first
(multiple--
) it prints:string(9) "name=nick"
For the URL of
/users/nicked-ic-ic-nicked-nicked-nicked-nicked-nicked-nicked-nicked-ack--last-first-middle-bla-bla-bla.html
it throws 404 since length of name is now > 50404 - Not found