php 试图在 Preg_Match 中找到正斜杠
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12239424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trying To Find Forward Slash In Preg_Match
提问by Go3Team
I've been searching for hours trying to find a solution to this. I am trying to determing if the REQUEST URI is legit and break it down from there.
我一直在寻找几个小时试图找到解决方案。我试图确定 REQUEST URI 是否合法并从那里分解它。
$samplerequesturi = "/variable/12345678910";
$samplerequesturi = "/variable/12345678910";
To determine if it is legit, the first section variableis only letters and is variable in length. The second section is numbers, which should have 11 total. My problem is escaping the forward slash so it is matched in the uri. I've tried:
为了确定它是否合法,第一部分variable只是字母并且长度可变。第二部分是数字,总共应该有 11 个。我的问题是转义正斜杠,因此它在 uri 中匹配。我试过了:
preg_match("/^[\/]{1}[a-z][\/]{1}[0-9]{11}+$/", $samplerequesturi)
preg_match("/^[\/]{1}[a-z][\/]{1}[0-9]{11}+$/", $samplerequesturi)
preg_match("/^#/#{1}[a-z]#/#{1}[0-9]{11}+$/", $samplerequesturi)
preg_match("/^|/|{1}[a-z]|/|{1}[0-9]{11}+$/", $samplerequesturi)
Among others which I can't remember now.
其他的我现在想不起来了。
The request usually errors out:
请求通常会出错:
preg_match(): Unknown modifier '|'
preg_match(): Unknown modifier '#'
preg_match(): Unknown modifier '['
Edit: I guess I should state that the REQUEST URI is already known. I'm trying to prove the whole string to make sure it isn't a bogus string ie to make sure there the 1st set is only lower case letters, and the 2nd set is only 11 numbers.
编辑:我想我应该声明 REQUEST URI 是已知的。我试图证明整个字符串以确保它不是伪造的字符串,即确保第一组只有小写字母,第二组只有 11 个数字。
回答by Niet the Dark Absol
/is not the only thing you can use as a delimiter. In fact, you can use almost any non-slphanumeric character. Personally I like to use ()because it reminds me that the first item of the result array is the entire match and it also never needs escaping in the pattern.
/不是唯一可以用作分隔符的东西。事实上,您几乎可以使用任何非数字字符。我个人喜欢使用,()因为它提醒我结果数组的第一项是整个匹配项,并且它也永远不需要在模式中转义。
preg_match("(^/([a-z]+)/(\d+)$)i",$samplerequesturi,$out);
var_dump($out);
That should do it.
那应该这样做。
回答by moopet
If you want to use regex (which I don't think is necessary in this case, simply splitting on "/" should be fine:
如果您想使用正则表达式(我认为在这种情况下没有必要,只需在“/”上拆分就可以了:
$samplerequesturi = "/variable/12345678910";
preg_match("@^/([A-Za-z]+)/(\d+)$@", $samplerequesturi, $out);
echo $out[1];
echo $out[2];
should get you going
应该让你去
回答by Bobulous
Your problem may be that you are using the /forward-slash as a regex delimiter (at the start and end of the regex expression). Switch to using a character other than the forward-slash, such as a #hash symbol or any other symbol which will never need to appear in this particular expression. Then you won't need to escape the forward-slash character at all in the expression.
您的问题可能是您使用/正斜杠作为正则表达式分隔符(在正则表达式的开头和结尾)。切换到使用除正斜杠以外的字符,例如#哈希符号或任何其他永远不需要出现在此特定表达式中的符号。然后您根本不需要在表达式中转义正斜杠字符。

