Traitement PHP lors de la restitution de l'encodage des caractères
De Typo3 CMS / Documentation Typo3 / Support Typo3.
Sommaire |
Introduction
Si le traitement de l'encodage des caractères de la base de données n'est pas fait au niveau de mysqldump, il existe une possibilité pour traiter l'affichage des caractères en PHP, voici les fonctions :
Fonction Convert UTF-8 to ISO8859-1
<?php function convertUTF8_to_8859($str) { // fonction qui test si la chaine est encodé en UTF8 if($this->is_utf8($str) == 1) { // fonction qui test si la chaine encodé en UTF8 contient des caractère français: Cette fonction ne traite que des chaines en UTF8 if($this->content8859_in_UTF8($str)=="TRUE") { // On convertit la chaine de UTF8 en ISO8859-1 $str = utf8_decode($str); // retourner la chaine converti return($str); }else{ // cas ou la chaine en UTF-8 mais ne contient pas des accents français (é,é,à,ù,û......) : exemple les caractères chinois encodé en UTF8 // retourner la chaine non convertit return($str); } }else{ // cas ou la chaine n'est pas encodé en UTF8 return($str); } } ?>
Fonction qui teste la présence de caractères français dans une chaine
<?php // fonction qui cherche s'il ya des caractères accentués français dans une chaine en UTF8 function content8859_in_UTF8($str) { if ( strlen($str) == 0 ) { return; } // cette fonction ne retourne de valeur si la chaine est en UTF8 // cette fonction retourne un tableau contenant les chaines accentuées preg_match_all('/.{1}|[^\x00]{1,1}$/us', $str, $ar); $chars = $ar[0]; $str_fr = 0; foreach ( $chars as $i => $c ) { $ud = 0; // Calcul les codes ASCII des chaines en UTF8 if (ord($c{0})>=0 && ord($c{0})<=127) { continue; } // ASCII - next please if (ord($c{0})>=192 && ord($c{0})<=223) { $ord = (ord($c{0})-192)*64 + (ord($c{1})-128); } if (ord($c{0})>=224 && ord($c{0})<=239) { $ord = (ord($c{0})-224)*4096 + (ord($c{1})-128)*64 + (ord($c{2})-128); } if (ord($c{0})>=240 && ord($c{0})<=247) { $ord = (ord($c{0})-240)*262144 + (ord($c{1})-128)*4096 + (ord($c{2})-128)*64 + (ord($c{3})-128); } if (ord($c{0})>=248 && ord($c{0})<=251) { $ord = (ord($c{0})-248)*16777216 + (ord($c{1})-128)*262144 + (ord($c{2})-128)*4096 + (ord($c{3})-128)*64 + (ord($c{4})-128); } if (ord($c{0})>=252 && ord($c{0})<=253) { $ord = (ord($c{0})-252)*1073741824 + (ord($c{1})-128)*16777216 + (ord($c{2})-128)*262144 + (ord($c{3})-128)*4096 + (ord($c{4})-128)*64 + (ord($c{5})-128); } if (ord($c{0})>=254 && ord($c{0})<=255) { $chars{$i} = $unknown; continue; } //error //Test si les caractères contient les accents (à, é,è,ù,ç,ê,â,û,........) if(($ord == 224) || ($ord == 226) || ($ord == 235) || ($ord == 249) || ($ord == 250) || ($ord == 252) || ($ord == 251) || ($ord == 233) || ($ord == 234) || ($ord == 232) || ($ord == 231) || ($ord == 228) || ($ord == 256) || ($ord == 128) || ($ord == 156) || ($ord == 230) || ($ord == 231) || ($ord == 244) || ($ord == 225) || ($ord == 236) || ($ord == 227) || ($ord == 237) || ($ord == 238) || ($ord == 249) || ($ord == 239) || ($ord == 257)){ $str_fr =1; } } if($str_fr == 1) { return "TRUE"; }else{ return "FALSE"; } } ?>
Fonction qui teste si le texte est en UTF-8
<?php // Returns true if $string is valid UTF-8 and false otherwise. function is_utf8($string) { // From http://w3.org/International/questions/qa-forms-utf-8.html return preg_match('%^(?: [\x09\x0A\x0D\x20-\x7E] # ASCII | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 )*$%xs', $string); } ?>
Les fonctions MultiByte String de PHP
Ces fonctions du cœur PHP sont aussi très utiles et efficaces pour le traitement des différents encodages.
Par exemple, mb_convert_encoding, qui teste une liste d'encodage sur une chaîne de caractère jusqu'à trouver le bon et le convertit dans l'encodage cible :
<?php //string mb_convert_encoding ( string $str , string $to_encoding [, mixed $from_encoding ] ) $content = mb_convert_encoding($content, 'ISO-8859-1', "ASCII, UTF-8, ISO-8859-1, ISO-8859-15");
addendum :
Dans le cas d'un mauvais retour de mb_detect_encoding,
il faut convertir la chaine via mb_convert_encoding avant de tester son encodage avec seemsUtf8
<?php $toConvert = "Montextepleindecaractèreslouches"; $convertedString = mb_convert_encoding($toConvert, 'ISO-8859-1', "ASCII, UTF-8, ISO-8859-1, ISO-8859-15"); if(!$this->seemsUtf8($convertedString)) $convertedString = $toConvert; public function seemsUtf8($string) { for ($i = 0; $i < strlen($string); $i++) { if (ord($string[$i]) < 0x80) continue; # 0bbbbbbb elseif ((ord($string[$i]) & 0xE0) == 0xC0) $n=1; # 110bbbbb elseif ((ord($string[$i]) & 0xF0) == 0xE0) $n=2; # 1110bbbb elseif ((ord($string[$i]) & 0xF8) == 0xF0) $n=3; # 11110bbb elseif ((ord($string[$i]) & 0xFC) == 0xF8) $n=4; # 111110bb elseif ((ord($string[$i]) & 0xFE) == 0xFC) $n=5; # 1111110b else return false; # Does not match any model for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ? if ((++$i == strlen($string)) || ((ord($string[$i]) & 0xC0) != 0x80)) return false; } } return true; }
