How to remove non-alphanumeric characters?
Regular expression is your answer.
$str = preg_replace('/[^a-z\d ]/i', '', $str);
- The
i
stands for case insensitive. ^
means, does not start with.\d
matches any digit.a-z
matches all characters betweena
andz
. Because of thei
parameter you don't have to specifya-z
andA-Z
.- After
\d
there is a space, so spaces are allowed in this regex.