note 71973 added to function.utf8-encode

From: Date: Wed, 27 Dec 2006 08:28:16 +0000
Subject: note 71973 added to function.utf8-encode
Groups: php.notes 
Request: Send a blank email to php-notes+get-120942@lists.php.net to get a copy of this message
la fonction c'est convertUTF8_to_8859() $str = "communauté et espérons"; $str_result = convertUTF8_to_8859($str); echo $str_result;// affichage : communauté espérons function convertUTF8_to_8859($str){ // fonction qui test si la chaine est encodé en UTF8 if(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(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); } } // 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); } // function is_utf8 // fonction qui cherche s'il ya des caractres accentus franais 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"; } } ---- Server IP: 217.174.196.197 Probable Submitter: 196.203.54.102 (proxied: 192.168.6.162, 196.203.54.102) ---- Manual Page -- http://www.php.net/manual/en/function.utf8-encode.php Edit -- https://master.php.net/note/edit/71973 Del: integrated -- https://master.php.net/note/delete/71973/integrated Del: useless -- https://master.php.net/note/delete/71973/useless Del: bad code -- https://master.php.net/note/delete/71973/bad+code Del: spam -- https://master.php.net/note/delete/71973/spam Del: non-english -- https://master.php.net/note/delete/71973/non-english Del: in docs -- https://master.php.net/note/delete/71973/in+docs Del: other reasons-- https://master.php.net/note/delete/71973 Reject -- https://master.php.net/note/reject/71973 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#120942) next »