Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save savankaneriya/b63f9db006c8c03fec210c9efffdffe2 to your computer and use it in GitHub Desktop.
Save savankaneriya/b63f9db006c8c03fec210c9efffdffe2 to your computer and use it in GitHub Desktop.
clean special characters or garbage characters from string to UTF-8 encoding
this function will convert distorted text or charactors to UTF-8 Encoding
function cleanString($text) {
$utf8 = array(
'/[áàâãªä]/u' => 'a',
'/[ÁÀÂÃÄ]/u' => 'A',
'/[ÍÌÎÏ]/u' => 'I',
'/[íìîï]/u' => 'i',
'/[éèêë]/u' => 'e',
'/[ÉÈÊË]/u' => 'E',
'/[óòôõºö]/u' => 'o',
'/[ÓÒÔÕÖ]/u' => 'O',
'/[úùûü]/u' => 'u',
'/[ÚÙÛÜ]/u' => 'U',
'/ç/' => 'c',
'/Ç/' => 'C',
'/ñ/' => 'n',
'/Ñ/' => 'N',
'/–/' => '-', // UTF-8 hyphen to "normal" hyphen
'/[’‘‹›‚]/u' => ' ', // Literally a single quote
'/[“”«»„]/u' => ' ', // Double quote
'/ /' => ' ', // nonbreaking space (equiv. to 0x160)
);
return preg_replace(array_keys($utf8), array_values($utf8), $text);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment