<?php
/*@@@
@ akam AT AkamENG dot COM
@ UTF-8 <--> Unicode Convertor
@ Supports 6 bit
@ For my Kurdish People!
*/
/*@@@@
*@ function Unicode_to_UTF: Decode Unicode Values to UTF-8
*@@@@*/
function Unicode_to_UTF( $input, $array=TRUE){
if(!is_array($input)){
$input = str_replace('&#', '', $input);
$input = explode(';', $input);
$len = count($input);
unset($input[$len-1]);
}else{
foreach($input as $k => $v){
$input[$k] = str_replace(array('&#', ';'), '', $v);
}
}
for($i=0; $i < count($input); $i++){
/* Decoding 1 bit
@@@@@@@@@@@@@@@@@@@@@@@@@@*/
if ( $input[$i] < 128 ){
$byte1 = $input[$i];
$utf[] = chr($byte1);
}
/* Decoding 2 bit
@@@@@@@@@@@@@@@@@@@@@@@@@@*/
if ( $input[$i] >= 128 && $input[$i] <= 2047 ){
$byte1 = 192 + (int)($input[$i] / 64);
$byte2 = 128 + ($input[$i] % 64);
$utf[] = chr($byte1).chr($byte2);
}
/* Decoding 3 bit
@@@@@@@@@@@@@@@@@@@@@@@@@@*/
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);
}
/* Decoding 4 bit
@@@@@@@@@@@@@@@@@@@@@@@@@@*/
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);
}
/* Decoding 5 bit
@@@@@@@@@@@@@@@@@@@@@@@@@@*/
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);
}
/* Decoding 6 bit
@@@@@@@@@@@@@@@@@@@@@@@@@@*/
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);
}
}
$utfout = '';
foreach($utf as $v){
$utfout .= $v;
}
return $utfout;
}
/*@@@@
*@ function UTF_to_Unicode: Encode UTF-8 Values to Unicode
*@@@@*/
function UTF_to_Unicode($input, $array=False) {
$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;
/* Encoding 1 bit
@@@@@@@@@@@@@@@@@@@@@@@@@@*/
if( $ints >= 0 && $ints <= 127 ){
// 1 bit
$value[] = $z;
$value1[]= dechex($z);
//$val[] = $value;
}
/* Encoding 2 bit
@@@@@@@@@@@@@@@@@@@@@@@@@@*/
if( $ints >= 192 && $ints <= 223 ){
// 2 bit
//$value[] = $temp = ($z-192) * 64 + $y;
$value[] = $temp = ($z-192) * 64 + $y;
$value1[]= dechex($temp);
//$val[] = $value;
}
/* Encoding 3 bit
@@@@@@@@@@@@@@@@@@@@@@@@@@*/
if( $ints >= 224 && $ints <= 239 ){
// 3 bit
$value[] = $temp = ($z-224) * 4096 + $y * 64 + $x;
$value1[]= dechex($temp);
//$val[] = $value;
}
/* Encoding 4 bit
@@@@@@@@@@@@@@@@@@@@@@@@@@*/
if( $ints >= 240 && $ints <= 247 ){
// 4 bit
$value[] = $temp = ($z-240) * 262144 + $y * 4096 + $x * 64 + $w;
$value1[]= dechex($temp);
}
/* Encoding 5 bit
@@@@@@@@@@@@@@@@@@@@@@@@@@*/
if( $ints >= 248 && $ints <= 251 ){
// 5 bit
$value[] = $temp = ($z-248) * 16777216 + $y * 262144 + $x * 4096 + $w * 64 + $v;
$value1[]= dechex($temp);
}
/* Encoding 6 bit
@@@@@@@@@@@@@@@@@@@@@@@@@@*/
if( $ints == 252 || $ints == 253 ){
// 6 bit
$value[] = $temp = ($z-252) * 1073741824 + $y * 16777216 + $x * 262144 + $w * 4096 + $v * 64 + $u;
$value1[]= dechex($temp);
}
/* Wrong Ord!
@@@@@@@@@@@@@@@@@@@@@@@@@@*/
if( $ints == 254 || $ints == 255 ){
echo 'Wrong Result!<br>';
}
}
if( $array === False ){
$unicode = '';
foreach($value as $value){
$unicode .= '&#'.$value.';';
}
//return str_replace(array('&#', ';'), '', $unicode);
return $unicode;
}
if($array === True ){
return $value;
}
}
?>