这篇文章主要为大家详细介绍了PHP Big5、Utf-8、GB2312 相互转码的简单示例,具有一定的参考价值,可以用来参考一下。
感兴趣PHP Big5、Utf-8、GB2312 相互转码的简单示例的小伙伴,下面一起跟随四海网的小编罗X来看看吧。<br>
/**
* @param
* @author 四海网 www.q1010.com
**/
// Big5 => GB
function b2g( $instr ) {
$fp = fopen( 'language/big5-gb.tab', 'r' );
$len = strlen($instr);
for( $i = 0 ; $i < $len ; $i++ ) {
$h = ord($instr[$i]);
if( $h >= 160 ) {
$l = ord($instr[$i+1]);
if( $h == 161 && $l == 64 )
$gb = ' ';
else {
fseek( $fp, (($h-160)*255+$l-1)*3 );
$gb = fread( $fp, 2 );
}
$instr[$i] = $gb[0];
$instr[$i+1] = $gb[1];
$i++;
}
}
fclose($fp);
return $instr;
}
// GB => BIG5
function g2b( $instr ) {
$fp = fopen( 'language/gb-big5.tab', 'r' );
$len = strlen($instr);
for( $i = 0 ; $i < $len ; $i++ ) {
$h = ord($instr[$i]);
if( $h > 160 && $h < 248 ) {
$l = ord($instr[$i+1]);
if( $l > 160 && $l < 255 ) {
fseek( $fp, (($h-161)*94+$l-161)*3 );
$bg = fread( $fp, 2 );
}
else
$bg = ' ';
$instr[$i] = $bg[0];
$instr[$i+1] = $bg[1];
$i++;
}
}
fclose($fp);
return $instr;
}
// Big5 => Unicode(UtF-8)
function b2u( $instr ) {
$fp = fopen( 'language/big5-unicode.tab', 'r' );
$len = strlen($instr);
$outstr = '';
for( $i = $x = 0 ; $i < $len ; $i++ ) {
$h = ord($instr[$i]);
if( $h >= 160 ) {
$l = ord($instr[$i+1]);
if( $h == 161 && $l == 64 )
$uni = ' ';
else {
fseek( $fp, ($h-160)*510+($l-1)*2 );
$uni = fread( $fp, 2 );
}
$codenum = ord($uni[0])*256 + ord($uni[1]);
if( $codenum < 0x800 ) {
$outstr[$x++] = chr( 192 + $codenum / 64 );
$outstr[$x++] = chr( 128 + $codenum % 64 );
# printf("[%02X%02X]<br>n", ord($outstr[$x-2]), ord($uni[$x-1]) );
}
else {
$outstr[$x++] = chr( 224 + $codenum / 4096 );
$codenum %= 4096;
$outstr[$x++] = chr( 128 + $codenum / 64 );
$outstr[$x++] = chr( 128 + ($codenum % 64) );
# printf("[%02X%02X%02X]<br>n", ord($outstr[$x-3]), ord($outstr[$x-2]), ord($outstr[$x-1]) );
}
$i++;
}
else
$outstr[$x++] = $instr[$i];
}
fclose($fp);
if( $instr != '' )
return join( '', $outstr);
}
// Unicode(UTF-8) => BIG5
function u2b( $instr ) {
$fp = fopen( 'language/unicode-big5.tab', 'r' );
$len = strlen($instr);
$outstr = '';
for( $i = $x = 0 ; $i < $len ; $i++ ) {
$b1 = ord($instr[$i]);
if( $b1 < 0x80 ) {
$outstr[$x++] = chr($b1);
# printf( "[%02X]", $b1);
}
elseif( $b1 >= 224 ) { # 3 bytes UTF-8
$b1 -= 224;
$b2 = ord($instr[$i+1]) - 128;
$b3 = ord($instr[$i+2]) - 128;
$i += 2;
$uc = $b1 * 4096 + $b2 * 64 + $b3 ;
fseek( $fp, $uc * 2 );
$bg = fread( $fp, 2 );
$outstr[$x++] = $bg[0];
$outstr[$x++] = $bg[1];
# printf( "[%02X%02X]", ord($bg[0]), ord($bg[1]));
}
elseif( $b1 >= 192 ) { # 2 bytes UTF-8
printf( "[%02X%02X]", $b1, ord($instr[$i+1]) );
$b1 -= 192;
$b2 = ord($instr[$i]) - 128;
$i++;
$uc = $b1 * 64 + $b2 ;
fseek( $fp, $uc * 2 );
$bg = fread( $fp, 2 );
$outstr[$x++] = $bg[0];
$outstr[$x++] = $bg[1];
# printf( "[%02X%02X]", ord($bg[0]), ord($bg[1]));
}
}
fclose($fp);
if( $instr != '' ) {
# echo '##' . $instr . " becomes " . join( '', $outstr) . "<br>n";
return join( '', $outstr);
}
}
// GB => Unicode(UTF-8)
function g2u( $instr ) {
$fp = fopen( 'language/gb-unicode.tab', 'r' );
$len = strlen($instr);
$outstr = '';
for( $i = $x = 0 ; $i < $len ; $i++ ) {
$h = ord($instr[$i]);
if( $h > 160 ) {
$l = ord($instr[$i+1]);
fseek( $fp, ($h-161)*188+($l-161)*2 );
$uni = fread( $fp, 2 );
$codenum = ord($uni[0])*256 + ord($uni[1]);
if( $codenum < 0x800 ) {
$outstr[$x++] = chr( 192 + $codenum / 64 );
$outstr[$x++] = chr( 128 + $codenum % 64 );
# printf("[%02X%02X]<br>n", ord($outstr[$x-2]), ord($uni[$x-1]) );
}
else {
$outstr[$x++] = chr( 224 + $codenum / 4096 );
$codenum %= 4096;
$outstr[$x++] = chr( 128 + $codenum / 64 );
$outstr[$x++] = chr( 128 + ($codenum % 64) );
# printf("[%02X%02X%02X]<br>n", ord($outstr[$x-3]), ord($outstr[$x-2]), ord($outstr[$x-1]) );
}
$i++;
}
else
$outstr[$x++] = $instr[$i];
}
fclose($fp);
if( $instr != '' )
return join( '', $outstr);
}
// Unicode(UTF-8) => GB
function u2g( $instr ) {
$fp = fopen( 'language/unicode-gb.tab', 'r' );
$len = strlen($instr);
$outstr = '';
for( $i = $x = 0 ; $i < $len ; $i++ ) {
$b1 = ord($instr[$i]);
if( $b1 < 0x80 ) {
$outstr[$x++] = chr($b1);
# printf( "[%02X]", $b1);
}
elseif( $b1 >= 224 ) { # 3 bytes UTF-8
$b1 -= 224;
$b2 = ord($instr[$i+1]) - 128;
$b3 = ord($instr[$i+2]) - 128;
$i += 2;
$uc = $b1 * 4096 + $b2 * 64 + $b3 ;
fseek( $fp, $uc * 2 );
$gb = fread( $fp, 2 );
$outstr[$x++] = $gb[0];
$outstr[$x++] = $gb[1];
# printf( "[%02X%02X]", ord($gb[0]), ord($gb[1]));
}
elseif( $b1 >= 192 ) { # 2 bytes UTF-8
printf( "[%02X%02X]", $b1, ord($instr[$i+1]) );
$b1 -= 192;
$b2 = ord($instr[$i]) - 128;
$i++;
$uc = $b1 * 64 + $b2 ;
fseek( $fp, $uc * 2 );
$gb = fread( $fp, 2 );
$outstr[$x++] = $gb[0];
$outstr[$x++] = $gb[1];
# printf( "[%02X%02X]", ord($gb[0]), ord($gb[1]));
}
}
fclose($fp);
if( $instr != '' ) {
# echo '##' . $instr . " becomes " . join( '', $outstr) . "<br>n";
return join( '', $outstr);
}
}
本文来自:http://www.q1010.com/173/2712-0.html
注:关于PHP Big5、Utf-8、GB2312 相互转码的简单示例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。
关键词:转码
四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。