PHP: Remove Everything But Letters And Numbers – Reg Expressions

PHP Scripts, Tips & Tricks


I always forget this PHP function, but I’m finding myself regularly needing it. I thought I’d just jot it down for my own reference and for anyone else who might need it.

Function: ereg_replace

What this function basically does is remove all characters from a string which isn’t a letter or a number. It can be a very cool function for error checking. Regular expression functions like this enable you to search for patterns within a string.

$string = "remove ever^&thing but *&^*&%£ letters & numbers*&^*";
$cleansedstring = ereg_replace("[^A-Za-z0-9]", "", $string );
echo $cleansedstring;

$cleansedstring should output: removeeverthingbutlettersnumbers

I used “[^A-Za-z0-9]” to remove every chracter that isn’t a letter or number, but here are some different matches:

[abc]a, b, or c
[a-z]Any lowercase letter
[^A-Z]Any character that is not a uppercase letter
(gif|jpg)Matches either “gif” or “jpeg”
[a-z]+One or more lowercase letters
[0-9\.\-]any number, dot, or minus sign
^[a-zA-Z0-9_]{1,}$Any word of at least one letter, number or _
([wx])([yz])wy, wz, xy, or xz
[^A-Za-z0-9]Any symbol (not a number or a letter)
([A-Z]{3}|[0-9]{4})Matches three letters or four numbers

You should note that because regular expressions are more powerful than regular functions, they are also slower. You should only use regular expressions if you have a particular need. Out of curiosity, does anyone know a less taxing function of getting the same result, or are regular expressions the most suitable option for stripping everything but numbers and letters?

For a more indepth explanation, here’s an awesome tutorial on Using Regular Expressions.

UPDATE: 04 Sept, 2009

Thanks to a reader, I’ve been informed about a more efficient way of stripping everything but alphanumeric characters.

$string = "remove ever^&thing but *&^*&%£ letters & numbers*&^*";
$cleansedstring = preg_replace('#\W#', '', $string);
echo $cleansedstring;



  1. Nick Overstreet spoke on 11/08/2008

    This is actually incorrect.
    Your posted example will have the exact opposite effect that you are intending it to here.
    Your replace will actually match ALL letters and numbers, and replace them with nothing. Leaving you a string with a bunch of random chars.

  2. Maruf spoke on 11/08/2008

    You’re wrong 🙂
    To double check, I just tried it out. Only numbers and letters remained.

  3. deniar spoke on 12/09/2008

    Thanks, i need this regular expression.

  4. Ben spoke on 16/02/2009

    Otherway would be to make an array of all unwanted characters then use an strreplace(), more long winded but faster.. who knows?

  5. Tom spoke on 30/03/2009

    This is perfect! Thanks. Worked like a charm.

  6. Daniel spoke on 04/08/2009

    Thank you verry much! You saved my ass. Works perfectly.

  7. BLOGERCISE spoke on 04/09/2009

    Maybe I don’t quite get what you are after, but if you just want to be left with alphanumeric characters then wouldn’t you use w?

    preg_replace(‘#\W#’, ”, $string)

  8. Maruf spoke on 04/09/2009

    Hey Blogercise,

    I’ve never come across that method before, but it works great!! Thanks a lot.

    I’ve updated the blog post 🙂

  9. Ian spoke on 27/10/2009

    Excellent, thanks!

  10. Chris spoke on 24/09/2011

    preg_replace(‘#\W#’, ”, $string) – simple, easy, effective… awesome man, been struggling this for ages today. A pretty cool tool for this is RegExBuddy – worth checking out if you ever need to build / test regexes and cant be bothered to do it using php.

  11. Marcio spoke on 02/06/2012

    Is there a way to use this regex with UTF-8 strings?

  12. c-note spoke on 15/07/2012

    It removes the spaces as well, not really ideal to clean a string.

  13. c-note spoke on 15/07/2012

    “/[^\w ]+/” retains the spaces

  14. James Wilson spoke on 15/08/2012

    But is there a way to do similar in MYSQL on a query?

    So you’d compare you pure text/numeric php variable, again a column that has had the same treatment on it?

    At the moment I have crappy Replaces on my column name:

    AND Replace(Replace(Replace(Replace(Replace(Location, ‘&’, ”), ‘-‘, ”), ‘ ‘, ”), ‘(‘, ”), ‘)’, ”)

Leave a reply

Your email address will not be published. Required fields are marked *