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
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
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???
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
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.
how if you want to have dot (.) kept in the string? what is your regular expression?
is it: /[^a-zA-Z0-9\s]+[^.]/
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);
A-HA The PROBLEM was that I DID cut and paste the code.
Swapped the fancy quotes for regular quotes and all is well.
thankyou, I am using in a php search function on an ink cartridge site
@PINOY
/[^a-zA-Z0-9.\s]/
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.
what if want to delete more than one space between the word?
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.
what if want to delete more than one space between the word?
try this
echo preg_replace(“/ {2,}/”, ” “, $string);
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 🙂
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!
@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/
how bout I wanted to remove the {} from {nja11234-1234123412-12341234-1234124qwer}
Hi,
THIS IS REALLY A NICE CODE .
IT SOLVES MY PROBLEM.
THANKS
Thank you so much
U R best in google results!
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//
Thanks it was great…..
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: http://www.my-wills-onlu.co.uk thanks
Thanks, I knew there was a simple solution for removing all the ( ) – . etc from phone numbers. I almost forgot regular expressions exist.
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: http://www.my-wills-only.co.uk thanks
Brilliant, thanks a lot
how to remove this character � ?
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)
Nice code, thanks you help me save the time 🙂
Hi,
thanks for nice code 🙂
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
@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.
just saved me a potential headache 🙂 simple and effective, thx
Thanks guys.. my problems has solved here 😀
thank you for sharing this post… solved my problem…
Hey very nice website!! Guy .. Beautiful .. Amazing .. I’ll bookmark your site and take the feeds also?I’m happy to seek out so many useful information here within the put up, we need work out extra techniques in this regard, thanks for sharing. . . . . .