Regular expressions

Oct17

Clean strings with regular expressions

17-Oct-2007
Filed under: PHP, Regular expressions

The following case-insensitive regular expression snippet will strip all non-alphanumeric characters from $string.

<?php
$string
= 'abc123_%$£';
$string = preg_replace("/[^a-z\d]/i", "", $string);
echo
$string; // Result: abc123
?>