Home  >  Article  >  Backend Development  >  PHP various encoding conversion implementation codes (1/6)_PHP tutorial

PHP various encoding conversion implementation codes (1/6)_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:54:491009browse

We provide several encoding conversions such as UTF-8 to GB encoding, GB to UTF-8 encoding, Unicode to utf8 unicode url encoding to gbk encoding function, GB code to Big5 code, Big5 code to GB code, utf8 to Unicode, etc. oh.

We provide several encoding conversions such as utf-8 to gb encoding, gb to utf-8 encoding, unicode to utf8, unicode url encoding to gbk encoding function, gb code to big5 code, big5 code to gb code, utf8 to unicode, etc. oh.
*/

$uc2gbtable = $codetable = $big5_data = $gb_data = '';
$gbkunidic = null;

//utf-8 to gb encoding
function utf82gb($utfstr)
{
if(function_exists('iconv'))
{
Return iconv('utf-8','gbk//ignore',$utfstr);
}
global $uc2gbtable;
$okstr = "";
if(trim($utfstr)=="")
{
Return $utfstr;
}
if(empty($uc2gbtable))
{
$filename = dedeinc."/data/gb2312-utf8.dat";
$fp = fopen($filename,"r");
while($l = fgets($fp,15))
{
$uc2gbtable[hexdec(substr($l, 7, 6))] = hexdec(substr($l, 0, 6));
}
fclose($fp);
}
$okstr = "";
$ulen = strlen($utfstr);
for($i=0;$i<$ulen;$i++)
{
$c = $utfstr[$i];
$cb = decbin(ord($utfstr[$i]));
if(strlen($cb)==8)
{
$csize = strpos(decbin(ord($cb)),"0");
for($j=0;$j < $csize;$j++)
{
$i++; $c .= $utfstr[$i];
}
$c = utf82u($c);
If(isset($uc2gbtable[$c]))
{
$c = dechex($uc2gbtable[$c]+0x8080);
$okstr .= chr(hexdec($c[0].$c[1])).chr(hexdec($c[2].$c[3]));
}
else
{
$okstr .= "".$c.";";
}
}
else
{
$okstr .= $c;
}
}
$okstr = trim($okstr);
return $okstr;
}

1 2 3 4 5 6

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631724.htmlTechArticleWe provide several encoding conversions such as UTF-8 to GB encoding GB to UTF-8 encoding Unicode to utf8 unicode url encoding to gbk encoding function GB code converted to Big5 code Big5 code converted to GB code utf8 converted to Unico...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn