How to remove non-alphanumeric characters?

zuk1 picture zuk1 · Mar 18, 2009 · Viewed 233.3k times · Source

I need to remove all characters from a string which aren't in a-z A-Z 0-9 set or are not spaces.

Does anyone have a function to do this?

Answer

Chad Birch picture Chad Birch · Mar 18, 2009

Sounds like you almost knew what you wanted to do already, you basically defined it as a regex.

preg_replace("/[^A-Za-z0-9 ]/", '', $string);