How to remove non alphanumeric chars from a string using php

Post Reply
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

How to remove non alphanumeric chars from a string using php

Post by Saman » Fri Jul 15, 2011 6:14 pm

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.

Code: Select all

<?php 

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

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

echo $new_string 

?>
Post Reply

Return to “PHP & MySQL”