I am trying to avoid regular backslash characters (don't ask - just say that I'm NOT trying to parse HTML :)) And I get something strange.
$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;
Spaces for clarity. This is the conclusion.
[ \ ^ $ . | ? * + ( ) \\ [ \\ \^ \& \. \| \? \* \+ \( \)
So, all is well, except for the first part. Where does "\\" come from and why is it not so "\ ["?
source share