Pass regex special characters through web form to PHP regex
I have a web form that does a search replace function.
The form input is passed through preg_quote. That's been fine, until now.
Now I want to allow users to input the * (asterisk) wildcard character.
preg_quote escapes it. How can I allow it through and properly apply it?
$find = ',*'; // I want to match a comma and all characters after the comma
$replace = ''; // replace with empty string
$find = preg_quote($find_replace[0],'~'); // $find ==
',\*';. I want * to remain unescaped. Is replacing
"\*" with "*" the best way?
if(strpos($find,'\\*') !== false) { // UPDATE: I just
tried this. It doesn't work
// $find == ',\*'
$find = str_replace('\\*','*',$find);
// $find == ',*' -- seems like it should work but doesn't
}
$value = trim(preg_replace('~' . $find .
'~i',$replace,$value));
EDIT: I added in code above that strips out the escaping slash before the
*. But it is not working.
With $value = 'Hey, hey, hey, hey' I end up with 'Hey hey hey hey'. The
commas are all that is removed, for some reason.
No comments:
Post a Comment