- 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);
来源: http://www.phpxs.com/code/1003471/