I'm trying to replace \
with \\
but it fails, it can't escape the \\
character.
My function call:
preg_replace('\', '\\', 'text to \ be parsed');
How can I escape all backslashes?
Use 4 backslashes and please don't forget the delimiters:
echo echo preg_replace('~\\\\~','\\\\\\\\','text to \\ be parsed');
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 \
.