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
Please rate this post by clicking a star:


(27 votes, average: 4.33 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.