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

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

Comment spam will be deleted.

This includes any comment posted here for the sole purpose of increasing your page rank. You're wasting my time and your own.