The Web Developer's Blog has been discontinued. I am now maintaining a new blog that is related to my company, Blue Piccadilly. The most popular articles from this blog (by monthly page views) have already been republished on the new blog, and indeed those original pages on this blog are being redirected to their new location. I'll probably keep these pages up here for a while. At least until the domain expires.

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
?>