25 février 2008 - 15:04Comment convertir un texte en UTF-8
voici une fonction simple mais efficace pour convertir un texte en UTF-8 sans se préoccuper de sa provenance (encodage d’origine).
<?php
setlocale(LC_ALL, 'fr_FR.utf8'); //selon la configuration du serveur cela peut être : fr_FR
mb_detect_order(array('UTF-8', 'ISO-8859-1', 'ISO-8859-15', 'Windows-1252')); //l'extention mb_string est installée par defaut sur php5
function toUTF8($string){
$from = mb_detect_encoding($string);
if ($from != 'UTF-8') {
$string = mb_convert_encoding($string, 'UTF-8', $from);
}
return $string;
} ?>
1 Commentaire | Catégorie : PHP / MySQL, Programmation |
Imprimer cet article

(5 votes, note : 4.8 sur 5)