PHP:转义 RegEx 保留字符 - 有人知道这有什么问题吗?
我试图用反斜杠转义正则表达式保留的字符(不要问——只要说我不想解析 HTML 就足够了:))而且我得到了一些奇怪的东西。
$regex_chars = array('[' , '\\' , '^', '$' , '.' , '|' ,
'?' , '*' , '+' , '(' , ')');
$regex_chars_escaped = array('\[ ' , '\\\\ ' , '\^ ', '\& ' ,
'\. ' , '\| ' , '\? ' , '\* ' , '\+ ' , '\( ' , '\)');
$escaped_string = str_replace($regex_chars,$regex_chars_escaped,
implode("",$regex_chars));
echo implode(' ',$regex_chars) . "<br />";
echo $escaped_string;
空格是为了清晰起见。这是输出
[ \ ^ $ . | ? * + ( )
\\ [ \\ \^ \& \. \| \? \* \+ \( \)
所以一切都很好,除了第一部分。 “\\”从哪里来,为什么不是“\[”?
I'm trying to escape regex-reserved characters with a backslash (don't ask -- suffice it to say I'm NOT trying to parse HTML :) ) And I'm getting something odd.
$regex_chars = array('[' , '\\' , '^', '
Spaces are for clarity. This is the output
[ \ ^ $ . | ? * + ( )
\\ [ \\ \^ \& \. \| \? \* \+ \( \)
So all is good, except for the first part. Where does the "\\" come from and why isn't it "\[" ?
, '.' , '|' ,
'?' , '*' , '+' , '(' , ')');
$regex_chars_escaped = array('\[ ' , '\\\\ ' , '\^ ', '\& ' ,
'\. ' , '\| ' , '\? ' , '\* ' , '\+ ' , '\( ' , '\)');
$escaped_string = str_replace($regex_chars,$regex_chars_escaped,
implode("",$regex_chars));
echo implode(' ',$regex_chars) . "<br />";
echo $escaped_string;
Spaces are for clarity. This is the output
So all is good, except for the first part. Where does the "\\" come from and why isn't it "\[" ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不简单地使用 preg_quote ?
Why not simply use preg_quote?
我相信这只是因为您将字符放入数组的顺序。试试这个:
你应该得到预期的输出。检查 str_replace 函数中的“潜在陷阱”部分规格
I believe it's just because of the order you're putting the chars in the array. Try this:
And you should get the expected output. Check the 'potential gotchas' section in the str_replace function spec