How do strip all regex special characters from a string?
eg: I have "/^bla\/bla\/bla\//i
" which I want to be: "bla/bla/bla/
"
How do strip all regex special characters from a string?
eg: I have "/^bla\/bla\/bla\//i
" which I want to be: "bla/bla/bla/
"
I don't understand exactly what you're trying to do, but if you just want to remove certain characters from a string you might be better off using strtr()
. For one thing it'll be much faster than doing a regex, and it'll probably be more readable as well.
edit: Actually I really don't understand what you're trying to achieve.
<?php
$string = "/^bla\/bla\/bla\//i";
$patterns = array(
"/\/\^([\w]+)/i" => "$1/",
"/[\/]{2,}i$/i" => "/",
"/\\\/" => "",
"/[\/]{2,}/" => "/",
"/\/$/" => "",
);
echo preg_replace(array_keys($patterns),$patterns,$string);
//OR, this:
echo "\n";
$string = "/^bla\/bla\/bla\//i";
$pattern = "/(?![\/\w]$)([\w]+)/";
preg_match_all($pattern,$string,$matches);
echo join('/',$matches[0]);
?>
I'm not sure if this answers your question, but are you maybe looking for the preg_quote ( http://us.php.net/manual/en/function.preg-quote.php ) function ?
I guess this is not really possible in the context I was thinking of. Thanks for your responses