27 September, 2010

How to remove non-ascii characters from string

There is a simple way to remove non-ascii characters from a string. This can be used to filter values from POST variables as well. Following line of code does the thing.

$string = "This is a test É";
//replace the non-ascii characters from string with nothing i.e. remove
preg_replace('/[^(\x20-\x7F]*/','',$string);
echo $string;

No comments: