How to remove single and double quotes from a string
I would not call that function string_sanitize()
, as it is misleading. You could call it strip_non_alphanumeric()
.
Your current function will strip anything that isn't an upper or lowercase letter or a number.
You can strip just '
and "
with...
$str = str_replace(array('\'', '"'), '', $str);
It looks like your original string had the HTML characters for "
("
) so when you attempt to sanitize it, you're simply remove the &
and ;
, leaving the rest of the string quot
.
---EDIT---
Probably the easiest way to remove non alpha numeric characters would be to decode the HTML characters with html_entity_decode, then run it through the regular expression. Since, in this case, you won't get anything that needs to be re-coded, you don't need to then do htmlentities, but it's worth remembering that you had HTML data and you now have raw unencoded data.
Eg:
function string_sanitize($s) {
$result = preg_replace("/[^a-zA-Z0-9]+/", "", html_entity_decode($s, ENT_QUOTES));
return $result;
}
Note that ENT_QUOTES
flags the function to "...convert both double and single quotes.".