php 实现UTF-8完美转换GB2312(GBK)的函数示例
内容摘要
这篇文章主要为大家详细介绍了php 实现UTF-8完美转换GB2312(GBK)的函数示例,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记。经测试代码
对此感兴趣的朋友,看看idc笔记做的技术笔记。经测试代码
文章正文
这篇文章主要为大家详细介绍了php 实现UTF-8完美转换GB2312(GBK)的函数示例,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记。经测试代码如下:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | <code class = "php" > /** * UTF-8完美转换GB2312(GBK) * * @param * @author php教程 www.idcnote.com **/ function GetGB2312String( $name ) { $tostr = "" ; for ( $i =0; $i < strlen ( $name ); $i ++) { $curbin = ord( substr ( $name , $i ,1)); if ( $curbin < 0x80) { $tostr .= substr ( $name , $i ,1); } elseif ( $curbin < bindec ( "11000000" )) { $str = substr ( $name , $i ,1); $tostr .= "&#" .ord( $str ). ";" ; } elseif ( $curbin < bindec ( "11100000" )) { $str = substr ( $name , $i ,2); $tostr .= "&#" .GetUnicodeChar( $str ). ";" ; $i += 1; } elseif ( $curbin < bindec ( "11110000" )) { $str = substr ( $name , $i ,3); $gstr = iconv( "UTF-8" , "GB2312" , $str ); if (! $gstr ) { $tostr .= "&#" .GetUnicodeChar( $str ). ";" ; } else { $tostr .= $gstr ; } $i += 2; } elseif ( $curbin < bindec ( "11111000" )) { $str = substr ( $name , $i ,4); $tostr .= "&#" .GetUnicodeChar( $str ). ";" ; $i += 3; } elseif ( $curbin < bindec ( "11111100" )) { $str = substr ( $name , $i ,5); $tostr .= "&#" .GetUnicodeChar( $str ). ";" ; $i += 4; } else { $str = substr ( $name , $i ,6); $tostr .= "&#" .GetUnicodeChar( $str ). ";" ; $i += 5; } } return $tostr ; } function GetUnicodeChar( $str ) { $temp = "" ; for ( $i =0; $i < strlen ( $str ); $i ++) { $x = decbin (ord( substr ( $str , $i ,1))); if ( $i == 0) { $s = strlen ( $str )+1; $temp .= substr ( $x , $s ,8- $s ); } else { $temp .= substr ( $x ,2,6); } } return bindec ( $temp ); } //程序使用: $value = GetGB2312String( $value ); </code> |
注:关于php 实现UTF-8完美转换GB2312(GBK)的函数示例的内容就先介绍到这里,更多相关文章的可以留意
代码注释