PHP preg_replace [英] PHP preg_replace
本文介绍了PHP preg_replace的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用netbeans,我尝试将\
替换为\\
,但失败了,它无法转义\\
字符.
I use netbeans, I try to replace \
with \\
but it fails , it can't escape the \\
character.
这不是Netbeans问题,而是PHP问题.
This is not a Netbeans issue , it's a PHP issue .
preg_replace('\','\\','text to \ be parsed');
有什么解决方法吗?
推荐答案
使用4个反斜杠,请不要忘记定界符:
Use 4 backslashes and please don't forget the delimiters:
echo echo preg_replace('~\\\\~','\\\\\\\\','text to \\ be parsed');
说明::当PHP解析\\\\
时,它将转义\\
两次,这意味着它将变为\\
,现在,当PHP将其传递给regex引擎时,它将收到\
.
Explanation: When PHP parse \\\\
it will escape \\
two times, which means it becomes \\
, now when PHP passes it to the regex engine, it will receive \\
which means \
.
这篇关于PHP preg_replace的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文