Remove all non-alphanumeric characters using preg_replace Remove all non-alphanumeric characters using preg_replace php php

Remove all non-alphanumeric characters using preg_replace


$url = preg_replace('/[^\da-z]/i', '', $string);


At first take this is how I'd do it

$str = 'qwerty!@#$@#$^@#$Hello%#$';$outcome = preg_replace("/[^a-zA-Z0-9]/", "", $str);var_dump($outcome);//string(11) "qwertyHello"

Hope this helps!


Not sure why no-one else has suggested this, but this seems to be the simplest regex:

preg_replace("/\W|_/", "", $string)

You can see it in action here, too: http://phpfiddle.org/lite/code/0sg-314