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…
1 2 3 4 | <?php $string_to_be_stripped = "Hi, I am a string and I need to be stripped..."; $new_string = ereg_replace("[^A-Za-z0-9]", "", $string_to_be_stripped ); ?> |
$new_string content will be: Hi I am a string and I need to be stripped without any coma or stop.
Enjoy.
27 Responses to “Strip non alphanumeric characters from a string with php”
Leave a Reply
- Citrus Engine released for free for learning
- My epic fail with ClickBank
- Get up to $100,000 for your next Flash game with Mochi GAME Developer Fund
- Create a dynamic content animated footer ad for your site in just 9 jQuery lines – 17 lines version
- Sell sitelocked version of your Flash games and even .fla sources to Free Online Games
- Protect your work from ActionScript code theft with SWF Protector
- Create a dynamic content animated footer ad for your site in just 9 jQuery lines
- Understanding Box2D’s one-way platforms, aka CLOUDS
- Triqui MochiAds Arcade plugin for WordPress upgraded to 1.2
- Box2D Flash game creation tutorial – part 2
- Create a Lightbox effect only with CSS - no javascript needed
- Flash game creation tutorial - part 1
- Create a Flash Racing Game Tutorial
- Flash game creation tutorial - part 2
- Make a Flash game like Flash Element Tower Defense - Part 2
- Flash game creation tutorial - part 3
- Make a Flash game like Flash Element Tower Defense - Part 1
- Create a flash draw game like Line Rider or others - part 1
- Triqui MochiAds Arcade plugin for WordPress official page
- Create a flash artillery game - step 1
- Flash game creation tutorial – part 5.2 (4.88/5)
- Create a flash artillery game – step 1 (4.79/5)
- Create a Flash Racing Game Tutorial (4.76/5)
- Create a survival horror game in Flash tutorial – part 1 (4.74/5)
- Create a flash artillery game – step 2 (4.74/5)
- Creation of a Flash arcade site using WordPress – step 2 (4.73/5)
- Flash game creation tutorial – part 1 (4.71/5)
- Flash game creation tutorial – part 2 (4.71/5)
- Create a flash draw game like Line Rider or others – part 1 (4.69/5)
- Creation of a platform game with Flash – step 2 (4.68/5)

(23 votes, average: 4.17 out of 5)



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.
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);
This page saved my behind this morning .. thanks to EVERYONE who contributed !
how about stripping only \n , \t characters only…?
This isn’t working for me for some reason. maybe preg_replace?
works great. thank you.
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;
}
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?!?
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!
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 …
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
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.
Now, how do I count characters with diacritics such as áéÃóú as valid letters (… and they’re expressed in UTF)
I tried
preg_replace(â€/[^A-Za-z0-9\s\s+]/â€,â€,$data);
and I got
Parse error: syntax error, unexpected ‘[‘
for Karl Groves:
to fix emanuele’s mistake you just need to add a space in the braclets, like this:
So that’s what a space looks like!
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!
Oral don’t copy past the code the quotation is wrong retype it and it workls fine.
Thanks. Useful info. It may also be useful to mention the str_replace function in PHP (http://au.php.net/str_replace). Also useful for replacing/stripping characters, but more useful in different contexts.
Thanks for this script!
most people would use trim($string,$charlist) to do this.
$new_string = ereg_replace(“[^A-Za-z0-9] “, “”, $string_to_be_stripped );
This will include spaces as well
Thank you. You are right about this code being hard to find on the Internet.
$result = preg_replace(“/[^A-Za-z0-9\s\s+\.\:\-\/%+\(\)\*\&\$\#\!\@\"\';\n\t\r]/”,”",$text);
Leaves almost every “normal” character. It’s my effort to nuke really weird characters that render as boxes in IE and question marks on a diamond background in Firefox.
well then try this
\\sioux & sioux[pass] *-/:, ? sioux
they all leave ‘extra’ chars except \\
Insta, YOU’RE A FREEKING LIFE SAVER
Yay! Thank you :)