Dreamhost

Looking for somewhere to host your site? Browse our PHP web hosts section!

Home > Code Snippets > Remove all characters except letters and numbers from a string

Remove all characters except letters and numbers from a string

Description

If you want to strip a string of all symbols and characters other than alphanumeric letters and numbers then use this. It will take a string and erase / delete any non-alphanumeric characters, and then output a clean version without the unwanted characters.

The code


<?php


$string 
"This is some text and numbers 12345 and symbols !£$%^&";

$new_string ereg_replace("[^A-Za-z0-9]"""$string);

echo 
$new_string

?>

Get the code

Download the file to your computer: Click here to get the file