Valid characters include the alphabet (abcd..), numbers (0123456789), spaces, ' and ".
I need to strip any other characters than these from a string in PHP.
Thanks :)
Valid characters include the alphabet (abcd..), numbers (0123456789), spaces, ' and ".
I need to strip any other characters than these from a string in PHP.
Thanks :)
You can do this:
$str = preg_replace('/[^a-z0-9 "\']/', '', $str);
Here the character class [^a-z0-9 "']
will match any character except the listed ones (note the inverting ^
at the begin of the character class) that are then replaced by an empty string.
Gumbo's answer is correct for your given specification. But if your "specification" is only "symbolic", what you eventually need might be like the following:
$str = preg_replace('{ [^ \w \s \' " ] }x', '', $str );
[^ ]
: negated character class (all except these inside)
\w
: alphanumeric (letters and digits)
\s
: white space
\'
: '
Regards
rbo