php 正则表达式和正斜杠
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3145264/
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
regular expression and forward slash
提问by pixeline
i'm searching for keywords in a string via a regular expression. It works fine for all keywords, exept one which contains a forward slash in it: "time/emit" .
我正在通过正则表达式在字符串中搜索关键字。它适用于所有关键字,除了一个包含正斜杠的关键字: "time/emit" 。
Even using preg_quote($find,'/'), which escapes it, i still get the message:
即使使用preg_quote($find,'/'), 逃脱它,我仍然收到消息:
Unknown modifier 't' in /frontend.functions.php on line 71
If i print the find pattern, it shows /time\\/emit/. Without preg_quote, it shows /time/emit/and both return the same error message.
如果我打印查找模式,它会显示/time\\/emit/. 如果没有 preg_quote,它会显示/time/emit/并且都返回相同的错误消息。
Any bit of knowledge would be useful.
任何一点知识都会有用。
回答by Kamil Szot
Try to begin and end your regular expression with different sign than /
尝试以与 / 不同的符号开始和结束正则表达式
I personally use `
我个人使用`
I've seen people using #
我见过人们使用#
I think most chars are good. You can read more about it here: http://pl.php.net/manual/en/regexp.reference.delimiters.php
我认为大多数字符都很好。您可以在此处阅读更多相关信息:http: //pl.php.net/manual/en/regexp.reference.delimiters.php
Like this:
像这样:
preg_match('#time/emit#', $subject); // instead of /time/emit/
To put it another way: Your $findvariable should contain rather #time/emit# than /time/emit/
换句话说:你的$find变量应该包含 #time/emit# 而不是 /time/emit/
回答by nathan
looks like you have something already escaping it..
看起来你已经有什么东西可以逃脱了..
preg_quote('time/emit') // returns time\/emit
preg_quote('time\/emit') // returns time\/emit
as a hack you could simply do:
作为一个黑客,你可以简单地做:
preg_quote(stripslashes($find)) // will return time\/emit
回答by John Ostrowick
this should work:
这应该有效:
$a="Hello////////";
$b=str_replace($a,"//","/");
echo $b;
回答by Ian Wood
bit of code?
一点代码?
the the 'regex' for that particular term should look something like '/time/emit/'. With a set of keywords there may be a more efficient method so seeing what you are doing would be good.
该特定术语的“正则表达式”应该类似于“/time/emit/”。使用一组关键字可能会有更有效的方法,所以看看你在做什么会很好。

