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 "\[" ?
We have given a string and we need to remove special characters from string str in PHP, for this, we have the following methods in PHP: Using str_replace() Method: The str_replace() method is used to remove all the special characters from the given string str by replacing these characters with the white space (” “).
By default, regular expressions will match any part of a string. It's often useful to anchor the regular expression so that it matches from the start or end of the string: ^ matches the start of string. $ matches the end of the string.
Why not simply use preg_quote?
I believe it's just because of the order you're putting the chars in the array. Try this:
$regex_chars = array('\\' , '[' , '^', '$' , '.' , '|' ,
'?' , '*' , '+' , '(' , ')');
$regex_chars_escaped = array( '\\\\ ' ,'\[ ', '\^ ', '\& ' ,
'\. ' , '\| ' , '\? ' , '\* ' , '\+ ' , '\( ' , '\)');
And you should get the expected output. Check the 'potential gotchas' section in the str_replace function spec
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With