Php Remove Non-Alphanumeric Characters

How to strip all symbols and numbers from a string of alphanumeric text. This php code will help you delete, remove, strip and erase any non-alphanumeric characters, and then return the data without the unwanted characters.

Need help removing un-wanted characters from your php text form strings. Here’s how to strip a string of all symbols and characters other than alphanumeric letters and numbers from your file or database. This code will delete any non-alphanumeric characters specified between the brackets, and then output/return the clean version.

The code

<?php $string = "Here! is some text, and numbers 12345, and symbols !£$%^&";
$new_string = preg_replace("/[^a-zA-Z0-9\s]/", "", $string);
echo $new_string ?>

The code should return this:

Here is some text and numbers 12345 and symbols

One more thing. If you don’t like to keep the blank white space in your text just reformat the preg_replace by removing the “\s” whitespace character types. This is good for use with user names and passwords. The code would now look something like this:


You may also like...

39 Responses

  1. Don Evans says:

    A very simple way of getting from 'September 23, 2009' to 'Sept/23/09' etc. Cures many ills when users don't quite follow the rules of entry.
    Good work.
    I'm using it in my code forward…
    Don Evans

  2. Andrew says:

    I couldn't key it to work with not removing spaces. I changed s to \s then it left the spaces in. Is this a different version of PHP???

  3. courtney says:

    saved my skin!!! i've been trying to get rid of the crap from my xml feed with no luck until i found this! thanks

  4. courtney says:

    how do u allow a fullstop to be left? am removing stuff from file names but i want the fullstop left so that the extension is maintained.

  5. pinoywebdev says:

    how if you want to have dot (.) kept in the string? what is your regular expression?

    is it: /[^a-zA-Z0-9\s]+[^.]/

  6. RR says:

    Hmmm – cut and pasted your code into an otherwise working script.
    I receive the following error:
    “Parse error: syntax error, unexpected ‘[‘ in {path} on line 139.”

    case “PA”:
    // Generate a Program Code
    // First remove numbers, symbols and spaces
    $alphastring = preg_replace(“/[^a-zA-Z0-9]/”, “”, $OrgName);

  7. RR says:

    A-HA The PROBLEM was that I DID cut and paste the code.

    Swapped the fancy quotes for regular quotes and all is well.

  8. thankyou, I am using in a php search function on an ink cartridge site

  9. FUNKER says:



  10. Deitrich says:

    Thanks! I am surprised that there is not a built in function for this. I am trying to get an article title to become a part of url in order to do so it first has to become alphanumeric then the spaces have to be replaced with something (dashes in my case). I wonder if there is a ready made function that I just have not came across yet.

    preg_replace will work for now. Thank you.

  11. elly says:

    what if want to delete more than one space between the word?

  12. I needed to work out the number of capital letters in a string and this helped strip out the punctuation so I only have the alphanumeric characters to work with.

    Many Thanks.


  13. pattaya says:

    what if want to delete more than one space between the word?

  14. adil says:

    try this
    echo preg_replace(“/ {2,}/”, ” “, $string);

  15. Achshar says:

    Hmm hey thanks simple abd usefull… but i came across some othjer type of symbols.. the ones which are not on keybord e.g the o with two small dots on top (call me dumb, but i dont know what they are called) but it even deals with them efficiently..

    thanks 🙂

  16. bender says:

    I have used this page many times in my apps now. Thank you.
    Although, PREG functions are slow compared to the other php string functions. Is there any way to do this “simple” function without the use of preg? I am just askin and not saying there is a better way. Thanks!

  17. Andy says:

    @Achshar: That would be an “Umlaut” above an o. There are loads of accented characters in non-english alphabets, the above shouldn’t really be used to deal with those as you remove the character entirely, leaving the string unreadable if it’s in a non-english language.

    There’s a function I use to convert all Accented characters into their latin alphabet equivalant. Seeing your comment makes me think I should blog it for everyone else’s use.

    So I have. Link here:

  18. aaron says:

    how bout I wanted to remove the {} from {nja11234-1234123412-12341234-1234124qwer}



  20. Saud says:

    Thank you so much
    U R best in google results!

  21. sandra says:

    estou com um probleminha.tenhu uma caixinha surpresa.tenhu q passar os numeros do decoder na caixinha alfha numericos.o problema e qdo a um zero.tem q digitar qual o valor
    1 e1 2 e 2 e o 0//

  22. Partha says:

    Thanks it was great…..

  23. simon says:

    looked at a few other similar examples and only this one worked as advised. used this code to strip punctuation out of user input postal addresses ready to create a will as a pdf. here is the site with your code in: thanks

  24. Owen Johnson says:

    Thanks, I knew there was a simple solution for removing all the ( ) – . etc from phone numbers. I almost forgot regular expressions exist.

  25. simon says:

    Amendment: looked at a few other similar examples and only this one worked as advised. used this code to strip punctuation out of user input postal addresses ready to create a will as a pdf. here is the site with your code in: thanks

  26. Adrian says:

    Brilliant, thanks a lot

  27. Richard says:

    how to remove this character � ?

    • Ricardo G. says:

      This will do the trick:

      $cleanString = preg_replace(‘/[\xZZ]/’,”, $dirtyString);

      (“ZZ” stands for the hex code of the original character – lots of them equate to this black diamond -once you visually locate it on an extended ASCII chart)

  28. Bash says:

    Nice code, thanks you help me save the time 🙂

  29. kht says:


    thanks for nice code 🙂

  30. Simon says:

    Dear all,

    I am newbie of php, i want to know how to add space between character and symbol or symbol and symbol, for example, the original string is “[PDF format(29.71MB)]”, how can i use preg_replace to represent the result as “[ PDF format (29.71MB) ]”?

    Many thanks


  31. bob says:

    @simon. why complicate when you can use str_replace. Just go learn a bit of php, dude, string -related functions are a must have, even for noobs.

    On the other hand, thanks for teh snippet, man. Fast and really useful.

  32. Chris says:

    just saved me a potential headache 🙂 simple and effective, thx

  33. yudiarif says:

    Thanks guys.. my problems has solved here 😀

  34. jamesmuga says:

    thank you for sharing this post… solved my problem…

  35. sakthi says:

    using for alpha in php

  36. sousstimes says:

    think’s for this methode :p

  37. Dan says:

    I’m new to Access 2010. How do I or where do I view code to insert this? I have an Access data base with phone numbers, some have dashes and ( ) while others dont, I want them all to show just the phone numbers.

  38. segun says:

    What about the ” symbol…had some issues with that….

Leave a Reply