Php Remove Non-Alphanumeric Characters

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

$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

 

 

Visit sunny St. George, Utah, USA