Reputation: 572
I'm attempting to use preg_replace to remove any characters in a string that fall outside the specified whitelist. The whitelist should contain all alphanumerics and common punctuation, as well as spaces and the following: ² ¹ º ° © ½ ¼ ¾ ® ™
I attempted to use the following to accomplish this:
preg_replace("/[^[:alnum:][:punct:] ²¹º°©½¼¾®™]/","", $string);
However, this removes everything on the string that comes after an illegal character, instead of removing ONLY the illegal characters. It works as desired if I only use the alnum, punct, and space character, so I think the problem lies with the special characters at the end - but I am having trouble figuring out how to include them properly.
Example:
"test test ₣ test test" becomes "test test ", but I want it to become "test test test test"
Upvotes: 2
Views: 213
Reputation: 70732
You want to use the u
(unicode) modifier.
$str = preg_replace('/[^[:alnum:][:punct:] ²¹º°©½¼¾®™]/u', '', $str);
Upvotes: 3