UTF-8 est un mécanisme standard utilisé par Unicode pour l'encodage de caractères.
- Code: Tout sélectionner
<?php
// Author akam at akameng dot com
// Support 6 bit
function UTF_to_Unicode($input, $array=False) {
$bit1 = pow(64, 0);
$bit2 = pow(64, 1);
$bit3 = pow(64, 2);
$bit4 = pow(64, 3);
$bit5 = pow(64, 4);
$bit6 = pow(64, 5);
$value = '';
$val = array();
for($i=0; $i< strlen( $input ); $i++){
$ints = ord ( $input[$i] );
$z = ord ( $input[$i] );
$y = ord ( $input[$i+1] ) - 128;
$x = ord ( $input[$i+2] ) - 128;
$w = ord ( $input[$i+3] ) - 128;
$v = ord ( $input[$i+4] ) - 128;
$u = ord ( $input[$i+5] ) - 128;
if( $ints >= 0 && $ints <= 127 ){
// 1 bit
$value .= '&#'.($z * $bit1).';';
$val[] = $value;
}
if( $ints >= 192 && $ints <= 223 ){
// 2 bit
$value .= '&#'.(($z-192) * $bit2 + $y * $bit1).';';
$val[] = $value;
}
if( $ints >= 224 && $ints <= 239 ){
// 3 bit
$value .= '&#'.(($z-224) * $bit3 + $y * $bit2 + $x * $bit1).';';
$val[] = $value;
}
if( $ints >= 240 && $ints <= 247 ){
// 4 bit
$value .= '&#'.(($z-240) * $bit4 + $y * $bit3 +
$x * $bit2 + $w * $bit1).';';
$val[] = $value;
}
if( $ints >= 248 && $ints <= 251 ){
// 5 bit
$value .= '&#'.(($z-248) * $bit5 + $y * $bit4
+ $x * $bit3 + $w * $bit2 + $v * $bit1).';';
$val[] = $value;
}
if( $ints == 252 && $ints == 253 ){
// 6 bit
$value .= '&#'.(($z-252) * $bit6 + $y * $bit5
+ $x * $bit4 + $w * $bit3 + $v * $bit2 + $u * $bit1).';';
$val[] = $value;
}
if( $ints == 254 || $ints == 255 ){
echo 'Wrong Result!<br>';
}
}
if( $array === False ){
return $unicode = $value;
}
if($array === True ){
$val = str_replace('&#', '', $value);
$val = explode(';', $val);
$len = count($val);
unset($val[$len-1]);
return $unicode = $val;
}
}
function Unicode_to_UTF( $input, $array=TRUE){
$utf = '';
if(!is_array($input)){
$input = str_replace('&#', '', $input);
$input = explode(';', $input);
$len = count($input);
unset($input[$len-1]);
}
for($i=0; $i < count($input); $i++){
if ( $input[$i] <128 ){
$byte1 = $input[$i];
$utf .= chr($byte1);
}
if ( $input[$i] >=128 && $input[$i] <=2047 ){
$byte1 = 192 + (int)($input[$i] / 64);
$byte2 = 128 + ($input[$i] % 64);
$utf .= chr($byte1).chr($byte2);
}
if ( $input[$i] >=2048 && $input[$i] <=65535){
$byte1 = 224 + (int)($input[$i] / 4096);
$byte2 = 128 + ((int)($input[$i] / 64) % 64);
$byte3 = 128 + ($input[$i] % 64);
$utf .= chr($byte1).chr($byte2).chr($byte3);
}
if ( $input[$i] >=65536 && $input[$i] <=2097151){
$byte1 = 240 + (int)($input[$i] / 262144);
$byte2 = 128 + ((int)($input[$i] / 4096) % 64);
$byte3 = 128 + ((int)($input[$i] / 64) % 64);
$byte4 = 128 + ($input[$i] % 64);
$utf .= chr($byte1).chr($byte2).chr($byte3).
chr($byte4);
}
if ( $input[$i] >=2097152 && $input[$i] <=67108863){
$byte1 = 248 + (int)($input[$i] / 16777216);
$byte2 = 128 + ((int)($input[$i] / 262144) % 64);
$byte3 = 128 + ((int)($input[$i] / 4096) % 64);
$byte4 = 128 + ((int)($input[$i] / 64) % 64);
$byte5 = 128 + ($input[$i] % 64);
$utf .= chr($byte1).chr($byte2).chr($byte3).
chr($byte4).chr($byte5);
}
if ( $input[$i] >=67108864 && $input[$i] <=2147483647){
$byte1 = 252 + ($input[$i] / 1073741824);
$byte2 = 128 + (($input[$i] / 16777216) % 64);
$byte3 = 128 + (($input[$i] / 262144) % 64);
$byte4 = 128 + (($input[$i] / 4096) % 64);
$byte5 = 128 + (($input[$i] / 64) % 64);
$byte6 = 128 + ($input[$i] % 64);
$utf .= chr($byte1).chr($byte2).chr($byte3).
chr($byte4).chr($byte5).chr($byte6);
}
}
return $utf;
}
?>

