Script de remplaçement d'accents

Tous les francophones savent à quel point il peut être difficile et énervant de travailler avec des accents en php. Il n'existe aucune fonction vraiment fiable pour stocker des accents dans nos bases de données ou lors de requêtes SQL, d'afficher des accents convenablement dans un navigateur.

Voici une fonction toute simple, mais qui remplace tous les accents par leur équivalent HTML...Qu'ils soient stocké dans une base de données ou affichés dans un navigateur, ce sera toujours un affichage correct.

 


function accentsToHtml($string)
{
    $string = str_replace("À", "À", $string);
    $string = str_replace("Á", "Á", $string);
    $string = str_replace("Â", "Â", $string);
    $string = str_replace("Ã", "Ã", $string);
    $string = str_replace("Ä", "Ä", $string);
    $string = str_replace("Å", "Å", $string);
    $string = str_replace("Æ", "&Aelig;", $string);
    $string = str_replace("à", "à", $string);
    $string = str_replace("á", "á", $string);
    $string = str_replace("â", "â", $string);
    $string = str_replace("ã", "ã", $string);
    $string = str_replace("ä", "ä", $string);
    $string = str_replace("å", "å", $string);
    $string = str_replace("æ", "æ", $string);
    $string = str_replace("Ç", "Ç", $string);
    $string = str_replace("ç", "ç", $string);
    $string = str_replace("Ð", "Ð", $string);
    $string = str_replace("ð", "ð", $string);
    $string = str_replace("È", "È", $string);
    $string = str_replace("É", "É", $string);
    $string = str_replace("Ê", "Ê", $string);
    $string = str_replace("Ë", "Ë", $string);
    $string = str_replace("è", "è", $string);
    $string = str_replace("é", "é", $string);
    $string = str_replace("ê", "ê", $string);
    $string = str_replace("ë", "ë", $string);
    $string = str_replace("Ì", "Ì", $string);
    $string = str_replace("Í", "Í", $string);
    $string = str_replace("Î", "Î", $string);
    $string = str_replace("Ï", "Ï", $string);
    $string = str_replace("ì", "ì", $string);
    $string = str_replace("í", "í", $string);
    $string = str_replace("î", "î", $string);
    $string = str_replace("ï", "ï", $string);
    $string = str_replace("Ñ", "Ñ", $string);
    $string = str_replace("ñ", "ñ", $string);
    $string = str_replace("Ò", "Ò", $string);
    $string = str_replace("Ó", "Ó", $string);
    $string = str_replace("Ô", "Ô", $string);
    $string = str_replace("Õ", "Õ", $string);
    $string = str_replace("Ö", "Ö", $string);
    $string = str_replace("Ø", "Ø", $string);
    $string = str_replace("Œ", "Œ", $string);
    $string = str_replace("ò", "ò", $string);
    $string = str_replace("ó", "ó", $string);
    $string = str_replace("ô", "ô", $string);
    $string = str_replace("õ", "õ", $string);
    $string = str_replace("ö", "ö", $string);
    $string = str_replace("ø", "ø", $string);
    $string = str_replace("œ", "œ", $string);
    $string = str_replace("Ù", "Ù", $string);
    $string = str_replace("Ú", "Ú", $string);
    $string = str_replace("Û", "Û", $string);
    $string = str_replace("Ü", "Ü", $string);
    $string = str_replace("ù", "ù", $string);
    $string = str_replace("ú", "ú", $string);
    $string = str_replace("û", "û", $string);
    $string = str_replace("ü", "ü", $string);
    $string = str_replace("Ý", "Ý", $string);
    $string = str_replace("Ÿ", "Ÿ", $string);
    $string = str_replace("ý", "ý", $string);
    $string = str_replace("ÿ", "ÿ", $string);
    return $string;
}