Created
May 10, 2013 01:43
-
-
Save hugollm/5551873 to your computer and use it in GitHub Desktop.
Brainstorming functions to convert diacritics into ASCII
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
/* | |
Convert common diacritics in portuguese language. | |
*/ | |
function convertPortugueseDiacritics($string) | |
{ | |
$from = 'áéíóúãõàèìòùçñüïÁÉÍÓÚÃÕÀÈÌÒÙÇÑÜÏ'; | |
$to = 'aeiouaoaeioucnuiAEIOUAOAEIOUCNUI'; | |
$from = preg_split('//u', $from, -1, PREG_SPLIT_NO_EMPTY); | |
$to = str_split($to); | |
$map = array_combine($from, $to); | |
return strtr($string, $map); | |
} | |
/* | |
Convert diacritics from all UTF-8 (as possible) | |
but leaves only letters, numbers and spaces, to | |
prevent conversion garbage. | |
*/ | |
function convertDiacritics($string) | |
{ | |
$string = iconv('UTF-8', 'US-ASCII//TRANSLIT', $string); | |
$string = preg_replace('|[^a-zA-Z0-9 ]|', '', $string); | |
return $string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment