Last active
June 29, 2018 10:53
-
-
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
this function will convert distorted text or charactors to UTF-8 Encoding |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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