php中gb2312跟unicode码的转换

// utf8 ---> unicode function utf8_unicode($c) { switch(strlen($c)) { case 1: return ord($c); case 2: $n = (ord($c[0]) & 0x3f) << 6; $n += ord($c[1]) & 0x3f; return $n; case 3: $n = (ord($c[0]) & 0x1f) << 12; $n += (ord($c[1]) & 0x3f) << 6; $n += ord($c[2]) & 0x3f; return $n; case 4: $n = (ord($c[0]) & 0x0f) << 18; $n += (ord($c[1]) & 0x3f) << 12; $n += (ord($c[2]) & 0x3f) << 6; $n += ord($c[3]) & 0x3f; return $n; } } //gb2312转换为unicode function gb2un($g)//传入gb2312字符串返回unicode码 { preg_match_all("/[/x80-/xff]?./",$g,$ar); $str = ""; foreach($ar[0] as $v) { $str = $str."&#".utf8_unicode(iconv("gb2312","utf-8",$v)).";"; } return $str; } //unicode ---> utf8 function unescape($str) { $str = rawurldecode($str); preg_match_all("/(?:%u.{4})| </div> <!-- Baidu Button BEGIN -->

0
0

你可能感兴趣的:(c,PHP,function)