Skip to content

Instantly share code, notes, and snippets.

@radicaldingos
Created October 28, 2015 15:33
Show Gist options
  • Save radicaldingos/8133be390374d620cc93 to your computer and use it in GitHub Desktop.
Save radicaldingos/8133be390374d620cc93 to your computer and use it in GitHub Desktop.
Function to strip accents of a string
<?php
/**
* Fonction permettant de se débarrasser des accents dans une chaîne de caractères,
* en les remplaçant par la lettre correspondante
*
* @param string $string Chaîne à nettoyer
*
* @return string Chaîne débarrassée d'accents
*/
function stripAccents($string){
return str_replace(
array('à','á','â','ã','ä','å','ç','è','é','ê','ë','ì',
'í','î','ï','ñ','ò','ó','ô','õ','ö','ø','ù','ú',
'û','ü','ý','ÿ','À','Á','Â','Ã','Ä','Ç','È','É',
'Ê','Ë','Ì','Í','Î','Ï','Ñ','Ò','Ó','Ô','Õ','Ö',
'Ù','Ú','Û','Ü','Ý'),
array('a','a','a','a','a','a','c','e','e','e','e','i',
'i','i','i','n','o','o','o','o','o','o','u','u',
'u','u','y','y','A','A','A','A','A','C','E','E',
'E','E','I','I','I','I','N','O','O','O','O','O',
'U','U','U','U','Y'),
$string
);
}
@radicaldingos
Copy link
Author

Cette solution est à privilégier par rapport à l'équivalent utilisant la fonction strtr - en tout cas avec PHP 5.6 ou antérieure - qui fonctionne bien tant qu'on utilise pas de chaîne encodée en UTF-8.

This solution is preferred over the equivalent using the ** strtr ** function - at least with PHP 5.6 or earlier - which works well as long as you do not use a UTF-8 encoded string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment