Last Updated: February 25, 2016
· aalaap
Aalaap filter 512

Remove all characters except letters and numbers in PHP

Receiving short codes in an app via URL query strings? Instead of using complex sanitization functions, this simple RegEx replace will get rid of all the junk in the input $string except for letters and numbers:

$clean_code = preg_replace('/[^\w]/', '', $string);

If you want more control over which character classes are preserved, you can specify them explicitly:

$clean_code = preg_replace('/[^a-zA-Z0-9]/', '', $string);
Say Thanks