未分类

php4.3.1以后的iconv函数很好用的

5 5月 , 2020  

php4.3.1以后的iconv函数很好用的。gb2312 和 unicode 间的编码转换 下面的例子是将 gb2312 转换为 “全”这种形式
php4.3.1以后的iconv函数很好用的,只是需要自己写一个uft8到unicode的转换函数
查表也行 复制代码 代码如下: $text =
“脚本之家”; preg_match_all(“/[x80-xff]?./”,$text,$ar); foreach
echo “”.utf8_unicode(iconv.”;”; ?> // utf8 -> unicode function
utf8_unicode { switch { case 1: return ord; case 2: $n = << 6; $n
+= ord & 0x3f; return $n; case 3: $n = << 12; $n += << 6; $n
+= ord & 0x3f; return $n; case 4: $n = << 18; $n += << 12;
$n += << 6; $n += ord & 0x3f; return $n; } } ?>
下面的例子是利用php将”全”这中编码转换为gb2312. 复制代码 代码如下:

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

//utf-8 转gb编码
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;
}

或者是 复制代码 代码如下: function
unescape { $str = rawurldecode;
preg_match_all|.{4};|d+;|.+/U”,$str,$r); $ar = $r[0]; print_r;
foreach { if == “%u”) $ar[$k] = iconv(“UCS-2″,”GB2312”,pack; elseif ==
“”) $ar[$k] = iconv(“UCS-2″,”GB2312”,pack); elseif == “”) { echo
substr.”
“; $ar[$k] = iconv(“UCS-2″,”GB2312”,pack; } } return join; } $str =
“TTL全天候自动聚焦”; echo unescape; //out TTL全天候自动聚焦
利用javascript来转换 复制代码 代码如下:

 

Unicode

 

下面是一个显示所有全角半角的字体的查看例子 复制代码 代码如下:

 

自定义: –

//gb转utf-8编码
function gb2utf8($gbstr)
{
 if(function_exists(‘iconv’))
 {
  return iconv(‘gbk’,’utf-8//ignore’,$gbstr);
 }
 global $codetable;
 if(trim($gbstr)==””)
 {
  return $gbstr;
 }
 if(empty($codetable))
 {
  $filename = dedeinc.”/data/gb2312-utf8.dat”;
  $fp = fopen($filename,”r”);
  while ($l = fgets($fp,15))
  {
   $codetable[hexdec(substr($l, 0, 6))] = substr($l, 7, 6);
  }
  fclose($fp);
 }
 $ret = “”;
 $utf8 = “”;
 while ($gbstr != ”)
 {
  if (ord(substr($gbstr, 0, 1)) > 0x80)
  {
   $thisw = substr($gbstr, 0, 2);
   $gbstr = substr($gbstr, 2, strlen($gbstr));
   $utf8 = “”;
   @$utf8 = u2utf8(hexdec($codetable[hexdec(bin2hex($thisw)) –
0x8080]));
   if($utf8!=””)
   {
    for ($i = 0;$i < strlen($utf8);$i += 3)
    $ret .= chr(substr($utf8, $i, 3));
   }
  }
  else
  {
   $ret .= substr($gbstr, 0, 1);
   $gbstr = substr($gbstr, 1, strlen($gbstr));
  }
 }
 return $ret;
}

下面是一个查表,转换gb2312到utf8的例子,
现在有iconv函数,这个已经没有太大的意义了, 复制代码 代码如下: function gb2utf8 return $gb;
$filename=”gb2312.txt”; $tmp=file; $codetable=array(); while=each
$codetable[hexdec]=substr; $utf8=””; while { if >127) {
$this=substr; $gb=substr;
$utf8.=u2utf8(hexdec($codetable[hexdec-0x8080])); }else{ $this=substr;
$gb=substr; $utf8.=u2utf8; } } return $utf8; } function u2utf8{ $str=””;
if { $str.=$c; } else if { $str.=chr; $str.=chr; } else if { $str.=chr;
$str.=chr; $str.=chr; } else if { $str.=chr; $str.=chr; $str.=chr;
$str.=chr; } return $str; } ?>

 

 

 

//unicode转utf8
function u2utf8($c)
{
 for ($i = 0;$i < count($c);$i++)
 {
  $str = “”;
 }
 if ($c < 0x80)
 {
  $str .= $c;
 }
 else if ($c < 0x800)
 {
  $str .= (0xc0 | $c >> 6);
  $str .= (0x80 | $c & 0x3f);
 }
 else if ($c < 0x10000)
 {
  $str .= (0xe0 | $c >> 12);
  $str .= (0x80 | $c >> 6 & 0x3f);
  $str .= (0x80 | $c & 0x3f);
 }
 else if ($c < 0x200000)
 {
  $str .= (0xf0 | $c >> 18);
  $str .= (0x80 | $c >> 12 & 0x3f);
  $str .= (0x80 | $c >> 6 & 0x3f);
  $str .= (0x80 | $c & 0x3f);
 }
 return $str;
}

//utf8转unicode
function utf82u($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[www.2979.com ,2]) & 0x3f) << 6;
   $n += ord($c[3]) & 0x3f;
   return $n;
 }
}

 

 

 

//big5码转换成gb码
function big52gb($text)
{
 if(function_exists(‘iconv’))
 {
  return iconv(‘big5′,’gbk//ignore’,$text);
 }
 global $big5_data;
 if(empty($big5_data))
 {
  $filename = dedeinc.”/data/big5-gb.dat”;
  $fp = fopen($filename, “rb”);
  $big5_data = fread($fp,filesize($filename));
  fclose($fp);
 }
 $max = strlen($text)-1;
 for($i=0;$i<$max;$i++)
 {
  $h = ord($text[$i]);
  if($h>=0x80)
  {
   $l = ord($text[$i+1]);
   if($h==161 && $l==64)
   {
    $gbstr = “ ”;
   }
   else
   {
    $p = ($h-160)*510+($l-1)*2;
    $gbstr = $big5_data[$p].$big5_data[$p+1];
   }
   $text[$i] = $gbstr[0];
   $text[$i+1] = $gbstr[1];
   $i++;
  }
 }
 return $text;
}

 

 

 

//gb码转换成big5码
function gb2big5($text)
{
 if(function_exists(‘iconv’))
 {
  return iconv(‘gbk’,’big5//ignore’,$text);
 }
 global $gb_data;
 if(empty($gb_data))
 {
  $filename = dedeinc.”/data/gb-big5.dat”;
  $fp = fopen($filename, “rb”);
  $gb = fread($fp,filesize($filename));
  fclose($fp);
 }
 $max = strlen($text)-1;
 for($i=0;$i<$max;$i++)
 {
  $h = ord($text[$i]);
  if($h>=0x80)
  {
   $l = ord($text[$i+1]);
   if($h==161 && $l==64)
   {
    $big = “ ”;
   }
   else
   {
    $p = ($h-160)*510+($l-1)*2;
    $big = $gb_data[$p].$gb_data[$p+1];
   }
   $text[$i] = $big[0];
   $text[$i+1] = $big[1];
   $i++;
  }
 }
 return $text;
}

 

 

 

//unicode url编码转gbk编码函数
function unicodeurl2gbk($str)
{
 //载入对照词典
 if(!isset($globals[‘gbkunidic’]))
 {
  $fp = fopen(dedeinc.’/data/gbk-unicode.dat’,’rb’);
  while(!feof($fp))
  {
   $globals[‘gbkunidic’][bin2hex(fread($fp,2))] = fread($fp,2);
  }
  fclose($fp);
 }

 //处理字符串
 $str = str_replace(‘$#$’,’+’,$str);
 $glen = strlen($str);
 $okstr = “”;
 for($i=0; $i < $glen; $i++)
 {
  if($glen-$i > 4)
  {
   if($str[$i]==’%’ && $str[$i+1]==’u’)
   {
    $uni = strtolower(substr($str,$i+2,4));
    $i = $i+5;
    if(isset($globals[‘gbkunidic’][$uni]))
    {
     $okstr .= $globals[‘gbkunidic’][$uni];
    }
    else
    {
     $okstr .= “&#”.hexdec(‘0x’.$uni).”;”;
    }
   }
   else
   {
    $okstr .= $str[$i];
   }
  }
  else
  {
   $okstr .= $str[$i];
  }
 }
 return $okstr;
}


相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图