Strip non alphanumeric characters from a string with php

Very simple and useful code, but hard to find in the internet.
You may need it when you code an user authentication script and want to prevent the user to hack it with ascii injection.
I will use regular expressions because it is the quickest way, and we want to be quick (almost) everytime...

PHP:
  1. <?php
  2. $string_to_be_stripped = "Hi, I am a string and I need to be stripped...";
  3. $new_string = ereg_replace("[^A-Za-z0-9]", "", $string_to_be_stripped );
  4. ?>

$new_string content will be: Hi I am a string and I need to be stripped without any coma or stop.

Enjoy.

Improve the blog rating this post
Tell me what do you think about this post. I'll write better and better entries.
1 Star2 Stars3 Stars4 Stars5 Stars (6 votes, average: 4 out of 5)
Loading ... Loading ...

17 Responses to “Strip non alphanumeric characters from a string with php”

  1. Karl Groves on November 23rd, 2006 3:17 pm

    Actually, as written, the new string will be HiIamastringandIneedtobestripped. The second parameter in ereg_replace (in your example) is not a space, but “nothing”, so the alpha characters will be joined together in one long word.

  2. trampolines on January 22nd, 2007 6:54 am

    also it’s really easy to do similar strips:

    only keep letters:
    $new_string = ereg_replace(”[^A-Za-z]“, “”, $string_to_be_stripped);

    keep alphanumerics and dashes
    $new_string = ereg_replace(”[^A-Za-z0-9-]“, “”, $string_to_be_stripped);

  3. Genealogy Dude on January 23rd, 2007 6:31 pm

    This page saved my behind this morning .. thanks to EVERYONE who contributed !

  4. Learn Hacking on March 24th, 2007 4:11 am

    how about stripping only \n , \t characters only…?

  5. Craigslist Autoposter on May 15th, 2007 11:34 pm

    This isn’t working for me for some reason. maybe preg_replace?

  6. chad on August 20th, 2007 6:17 pm

    works great. thank you.

  7. roberto on November 10th, 2007 3:49 am

    Thanks, I just improved that a bit, replacing spaces with a dash just before stripping non alphanum (or dashes).

    This is useful to let users add valid “subdomains” strings.

    I use this inside a class with my other “tools” :)

    public static function stripText($text)
    {
    $text = strtolower(trim($text));
    // replace all white space sections with a dash
    $text = str_replace(’ ‘, ‘-’, $text);
    // strip all non alphanum or -
    $clean = ereg_replace(”[^A-Za-z0-9\-]“, “”, $text);

    return $clean;
    }

  8. Mark on November 20th, 2007 4:11 am

    You’ve got to be kidding. This is the top google search for “php strip characters” and the post doesn’t even contain correct info?!?

  9. Dave Cobbe on January 27th, 2008 4:55 pm

    Dude– that’s cool! Also on the hunt for a “html safe” function. Thinking of having custom blogs and incorporating limited HTML tags in the body (to include images, change fonts, etc) but would like to replace tags with hex encoded characters … anyway, sure I’ll find that somewhere — meanwhile; thanks for the regular expression. Sweet!

  10. Dave Cobbe on January 27th, 2008 4:59 pm

    PS. re: Mark above — that’s a problem with Google, not this post! Last time I searched “Lithuanian Universities” I got up “Singapore”. Google! One day the revolution will come …

  11. Dragos on March 26th, 2008 6:44 pm

    I use this to strip any character except alphanumeric chars and spaces.
    $data=preg_replace(”/[^A-Za-z0-9\s\s+]/”,”,$data);
    _____________________________
    http://websitetools.110mb.com

  12. Binky on April 7th, 2008 11:46 pm

    Keep in mind that if you copy and paste code from this page into your code, it WILL NOT work because of slight formatting changes imposed by this page. For example, the simple double quote character is replaced by the fancy left & right slanted versions, which PHP doesn’t recognize as quotes. So paste and then check the details, then test and pay attention to any error messages.

  13. alejandro on May 9th, 2008 8:59 pm

    Now, how do I count characters with diacritics such as áéíóú as valid letters (… and they’re expressed in UTF)

  14. oral on June 23rd, 2008 7:22 pm

    I tried

    preg_replace(”/[^A-Za-z0-9\s\s+]/”,”,$data);

    and I got

    Parse error: syntax error, unexpected ‘[’

  15. Erik on June 24th, 2008 6:35 pm

    for Karl Groves:
    to fix emanuele’s mistake you just need to add a space in the braclets, like this:

  16. Jon on August 19th, 2008 4:33 pm

    So that’s what a space looks like!

  17. Mike on September 28th, 2008 1:21 pm

    This saved my life.

    My script that handles the creation of license files that are used to unlock a plugin I sell wasn’t stripping non ASCII’s, and whaddya know - non ASCII’s made the unlock-function within my plugin barf.

    Thanks!

Leave a Reply