http://www.php.net/manual/en/function.preg-replace.php

codice:
echo preg_replace('#[^a-z0-9_]#i','',$str);