I use netbeans, I try to replace \\
with \\\\
but it fails , it can\'t escape the \\\\
character.
This is not a Netbeans issu
Try the php chr()
function and tell the preg_replace the char ascii code for \
and \\
.
chr function
ascii code table
<?php
echo chr(52) . "<br>"; // Decimal value
echo chr(052) . "<br>"; // Octal value
echo chr(0x52) . "<br>"; // Hex value
preg_replace(chr(1),chr(2),'text'),
?>
This works: (using str_replace() rather than preg_replace())
$str = "text to \ be parsed";
$str = str_replace('\\', '\\\\', $str);
echo $str;
Use 4 backslashes and please don't forget the delimiters:
echo echo preg_replace('~\\\\~','\\\\\\\\','text to \\ be parsed');
Online demo
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 \
.