note 84136 added to function.utf8-encode
| From: | akam at osu1 dot php dot net | Date: | Mon, 30 Jun 2008 14:14:09 +0000 |
| Subject: | note 84136 added to function.utf8-encode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-141008@lists.php.net to get a copy of this message | ||
<?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;
}
?>
----
Server IP: 208.69.120.35
Probable Submitter: 78.111.134.30
----
Manual Page -- http://www.php.net/manual/en/function.utf8-encode.php
Edit -- https://master.php.net/note/edit/84136
Del: integrated -- https://master.php.net/note/delete/84136/integrated
Del: useless -- https://master.php.net/note/delete/84136/useless
Del: bad code -- https://master.php.net/note/delete/84136/bad+code
Del: spam -- https://master.php.net/note/delete/84136/spam
Del: non-english -- https://master.php.net/note/delete/84136/non-english
Del: in docs -- https://master.php.net/note/delete/84136/in+docs
Del: other reasons-- https://master.php.net/note/delete/84136
Reject -- https://master.php.net/note/reject/84136
Search -- https://master.php.net/manage/user-notes.php