PHP: Escaping RegEx-reserved characters - anyone know what's wrong with this?
php, regex, str-replace
Solution
Why not simply use preg_quote?
Problem
I'm trying to escape regex-reserved characters with a backslash (don't ask -- suffice it to say I'm NOT trying to parse HTML :) ) And I'm getting something odd. ``` $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 are for clarity. This is the output ``` [ \ ^ $ . | ? * + ( ) \\ [ \\ \^ \& \. \| \? \* \+ \( \) ``` So all is good, except for the first part. Where does the "\\" come from and why isn't it "\[" ?