Skip to content

Instantly share code, notes, and snippets.

@hugollm
Created May 10, 2013 01:43
Show Gist options
  • Save hugollm/5551873 to your computer and use it in GitHub Desktop.
Save hugollm/5551873 to your computer and use it in GitHub Desktop.
Brainstorming functions to convert diacritics into ASCII
/*
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