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:

Hereissometextandnumbers12345andsymbols

Tagged : / / / / /

36 thoughts on “Php Remove Non-Alphanumeric Characters

  1. 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. 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.”

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

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

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

  4. 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.

  5. 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.

    Chris.

  6. 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 🙂

  7. 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!

  8. @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: http://myshadowself.com/coding/php-function-to-convert-accented-characters-to-their-non-accented-equivalant/

  9. 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//

    1. 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)

  10. 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

    Simon

  11. @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.

Leave a Reply

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