Reputation: 667
I have the below function with str_replace
:
function url_seo($text)
{
$match = array(']','\\',';',"'",',','.','/','~','`','=');
$replace = array('-','','','','','','','','','');
$text = str_replace($match, $replace, $text);
return $text;
}
Should I place a \
before all the characters in the $match
variable like this:
$match = array('\]','\\','\;',"\'",'\,','\.','\/','\~','\`','\=');
or there is no need to?
Upvotes: 1
Views: 15040
Reputation: 626896
According to the str_replace PHP reference,
search
The value being searched for, otherwise known as the needle. An array may be used to designate multiple needles.
So, this is no regular expression, and you do not have to escape any characters except the escape sequences that must always be escaped.
\"
Print the next character as a double quote, not a string closer
\'
Print the next character as a single quote, not a string closer
\n
Print a new line character
\t
Print a tab character
\r
Print a carriage return (not used very often)
\$
Print the next character as a dollar, not as part of a variable
\\
Print the next character as a backslash, not an escape character
So, only \
in your array of strings should be escaped.
Depending on the string qualifier symbol, you may also have to escape '
and "
, but you can just alternate them as you already did: '"'
and "'"
is a good way to avoid over-escaping.
Upvotes: 10