正则表达式和正斜杠 [英] regular expression and forward slash
问题描述
我正在通过正则表达式搜索字符串中的关键字.它适用于所有关键字,例如其中一个包含正斜杠的关键字"time/emit".
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" .
即使使用preg_quote($find,'/')
进行了转义,我仍然收到消息:
Even using preg_quote($find,'/')
, which escapes it, i still get the message:
Unknown modifier 't' in /frontend.functions.php on line 71
如果我打印查找图案,它会显示/time\\/emit/
.如果没有preg_quote,则显示/time/emit/
,并且都返回相同的错误消息.
If i print the find pattern, it shows /time\\/emit/
. Without preg_quote, it shows /time/emit/
and both return the same error message.
任何知识都是有用的.
推荐答案
尝试以与/
我个人使用`
我见过人们在使用#
我认为大多数字符都很好.您可以在此处了解更多信息: http://pl.php.net /manual/zh-CN/regexp.reference.delimiters.php
I think most chars are good. You can read more about it here: http://pl.php.net/manual/en/regexp.reference.delimiters.php
喜欢这个:
preg_match('#time/emit#', $subject); // instead of /time/emit/
换句话说:您的$find
变量应该包含/time/emit/
To put it another way: Your $find
variable should contain rather #time/emit# than /time/emit/
这篇关于正则表达式和正斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!