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


(96 votes, average: 4.61 out of 5)
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…
using for alpha in php
think’s for this methode :p
Motorola Droid finally rooted! The Motorola Droid may be the first Android 2. 0 with an excellent high-resolution screen, a slim shape, a physical qwerty keyboard and Google’s latest build of Android (the operating system). The net is richer. Photos are sharper. Videos pop. It has got the innovative browser with a blazing-fast? cessor and 3G speed for faster search. It has got the world’s thinnest QWERTY slider at 13. 7mm. It can benefit you navigate turn-by-turn. It includes a 5MP camera with a flash, plus lots of great camera features and a DVD-quality recorder to fully capture all of the richer, bigger, wider experiences you are going to have.
Multimedia capabilities
Motorola Droid is coming with the integral media player that supports AAC, H. 263, H. 264, MP3, MPEG-4, WAV, WMA, eAAC+, OGG, AMR WB, AMR NB, AAC+, MIDI. Regrettably, there is absolutely no syncing computer software that will help you manage and transfer your music. Since it is at this time, you need to make use of the old drag-and-drop method utilizing the USB cable or sideload them utilizing a microSD card.
Obviously, you may also down load songs via the Amazon MP3 store. The store enables you to browse by album, song, artist, or genre. You are able to down load the DRM-free songs over Wi-Fi in addition to Verizon’s 3G network, although the Droid advises you switch to Wi-Fi when possible since it’s faster.
The bad is, Motorola Droid isn’t competent to run and read your DVD disc. And, for videos, even it supports MP4, AVI continues to be the primary blast of video container at this time. Then, so what can you do in the event that you curently have an accumulation of digital video content in AVI, MKV MOV, MOD, M2TS, and so on, or on DVD disc, and also you are wanting to watch them in your lovely Motorola Droid?
Below, I shall demonstrate how exactly to convert videos to Motorola Droid. Computer software you’ll need
Since Motorola Droid isn’t compatible to see AVI, WMV, MPEG-2, and so on, here we shall require a 3rd computer software to help us to convert video format. The type of video converters, I’d like to just take Full Video Converter because of its forward-straight GUI and full functionalities. only with Full Video Converter?? we are able to easily finish the DVD conversion and video conversion for Motorola Droid. You are able to follow this connect to down load Full Video Converter for an effort.
Get Start
1. Add files
Click Add File(s) button on the toolbar, select video or audio recordings you wish to convert in the dialog box that opens, and add them to the file list.
2. Select output format
Choose a number of files in the file list, and open Profile drop-down list on the left, then select an output format As "Android", You’ll be able to open Profile drop-down list on the best to pick profile of "Motorola Droid"
Tip: To regulate the output settings, click Edit button alongside Profile drop-down list, and set the parameters of output format in the Conversion Setting dialog box that opens.
3. Set output folder
Click Browse button in setting panel, pick a destination folder in the dialog box that opens, and click O.K..
Tip: Before begin to convert video, you are able to clip video segment, crop area size, add effects, and create video watermark to colorize your output file.
4. You are able to click on the Edit’s Buttons to edit the video as to the you prefer.
5. Convert file
After finishing the above mentioned settings, always check the files needed to convert in the file list, and click Convert Checked Item(s) button on the toolbar to begin converting the files. You will see the conversion progress from the progress bar at the end of the window.
Tip: You are able to click Pause Checked Item(s) button to pause the converting process and prevent Checked Item(s) button to cancel the procedure as required.
Finally, We recommoned that using Full Video Converter to convert video.
It’s just very easy to convert DVD, video to Motorola Droid with Full Video Converter?. Now, release you to ultimately the digital world together with your lovely Motorola Phone!
Resource:How to convert DVD-Video to Motorola Droid
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.