Tuesday, April 26, 2011

Removing garbage(non ascii) character from string

When you copy and paste some data from some source then sometime you got some non ascii garbage values like

♠ ♠

♣ ♣

♥ ♥ etc.


You can remove this using preg_replace function of php

$name="Sandeep♠ Singh♣ Bisht♥";
$name = preg_replace('/[^(\x20-\x7F)]*/','', $name);

echo $name;


Your input is : Sandeep♠ Singh♣ Bisht♥

And the output is :Sandeep Singh Bisht


For more reference:

http://php.net/manual/en/function.preg-replace.php
http://www.stemkoski.com/php-remove-non-ascii-characters-from-a-string/

No comments: