PHP preg_replace不是单词字符(\ W)的字符除了:“ - ”,“。”等

I've searched for quite a while on this one, I want to filter a string in PHP to allow only word characters and certain other characters such as "-",".", etc..

preg_replace(/\W+/,'',$str)

For all the non-word characters it works great, adding the "except from" causes trouble.

Also, if you have a source that explains the subject thorough, I would be happy to get to the bottom of it. The subject is either very complicated or I've read bad explanations of it.

Use a negated character class that includes word characters and the other characters you want to allow.

$new_str = preg_replace('/[^-,.\w]+/', '', $str);

Lots of information about regular expressions can be found at www.regular-expressions.info.