Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Escaping RegEx-reserved characters - anyone know what's wrong with this?

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('&nbsp;',$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 "\[" ?

like image 729
Greg Avatar asked Nov 24 '09 11:11

Greg


People also ask

How do I remove escape characters from data in PHP?

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 (” “).

What will the regular expression match?

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.


2 Answers

Why not simply use preg_quote?

like image 87
Bart Kiers Avatar answered Sep 19 '22 14:09

Bart Kiers


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

like image 21
danii Avatar answered Sep 20 '22 14:09

danii