Im testing on this website: Regex Tester
And I came up with this expression /[\w ]/
that does exactly what I want on that website, which is to select special characters.
BUT when I use it in my PHP file it isnt working.
In my PHP i have this function:
function removeSpecialChar($str) {
return str_replace("/[^\w ]/", "", $str);
}
For example lets say that $str="asdf1234 !"#¤asd"
It should return "asdf1234 asd"
but it isnt, any ideas?
Use preg_replace when you need the regex-based replacement. str_replace will treat the given string as is - without any special meaning given to metacharacters.
$str = 'asdf1234 !\"#¤asd';
function removeSpecialChar($str) {
return preg_replace('/[^\w ]/', '', $str);
}
echo removeSpecialChar($str); // asdf1234 asd