bn47ka
Last Updated: February 25, 2016
·
9.697K
· 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
Respond