Home >php教程 >php手册 >编码对照表的使用(1)

编码对照表的使用(1)

WBOY
WBOYOriginal
2016-06-21 09:11:451130browse

编码

/**
* 当制作好编码对照表后,并不能马上使用还需对其进行一些加工
* 当然简单的查表是可以的但效率不高
* 现在以繁简转换为例,说明对照表的使用
*/

/**
* 连接并打开对照表
*/
$conn = mysql_connect();
mysql_select_db("unicode");
/**
* 提取其中big5和gbk编码,表中big5只有13487个(包括符号)
*/
$sql = "select gbk,big5,gb2312 from unicode where big5 != ''";
//$sql = "select * from unicode";
$rs = mysql_query($sql);
echo mysql_num_rows($rs)."
";
/**
* 读取数据到数组,注意key和value的处理
*/
$code = array(0=>"gb",big5=>0);

while($row = mysql_fetch_array($rs)) {
  $code[pack("H4",$row][gbk])] = pack("H4",$row[big5]);
//  $code[$row][gb2312]] = pack("H4",$row[big5]);
}

mysql_close($conn);

function gb_big5($s) {
  global $code;
  $v = "";
  if($code[0] == "big5")
    $code = $arr_flip($code);
  for($i=0;$i    if(ord($s[$i]) > 0x7f) {
      $p = sprintf("%02X%02X",$s[$i],$s[$i+1]);
      $p = $s[$i].$s[$i+1];
      $ch = $code[$p];
      if($ch == "")
        $ch = $s[$i].$s[$i+1];
      $v .= $ch;
      $i++;
    }else
      $v .= $s[$i];
  }
  return $v;
}
echo '';
echo gb_big5('

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