I use netbeans, I try to replace \ with \\ but it fails , it can't escape the \\ character.
This is not a Netbeans issue , it's a PHP issue .
preg_replace('\','\\','text to \ be parsed');
Any sollutions?
I use netbeans, I try to replace \ with \\ but it fails , it can't escape the \\ character.
This is not a Netbeans issue , it's a PHP issue .
preg_replace('\','\\','text to \ be parsed');
Any sollutions?
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 \.
Try the php chr() function and tell the preg_replace the char ascii code for \ and \\.
<?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;