search
Homephp教程php手册php的汉字转换: GBK->Unicode(UTF8)

汉字|转换

php的汉字转换一直是比较麻烦的事

该类内置了四个函数"htmlHex","htmlDec","escape","u2utf8"
方便用户的使用,同时也可自定义函数进行自己喜欢的操作

qswhGBK.php 从这里下载
http://www.blueidea.com/user/qswh/qswhGBK.zip


class qswhGBK{
 var $qswhData;
 function qswhGBK($filename="qswhGBK.php"){
  $this->qswhData=file($filename);
 }
 function gb2u($gb,$callback=""){
  /******(qiushuiwuhen 2002-8-15)******/
  $ret="";
  for($i=0;$i   if(($p=ord(substr($gb,$i,1)))>127){
   
    $q=ord(substr($gb,++$i,1));
    $q=($q-($q>128?65:64))*4;
    $q=substr($this->qswhData[$p-128],$q,4);
   }
   else
    $q=dechex($p);
   if(empty($callback))
    $ret.=$q;
   else {
    $arr=array("htmlHex","htmlDec","escape","u2utf8");
    if(is_integer($callback)){
     if($callback>count($arr))die("Invalid Function");
     $ret.=$this->$arr[$callback-1]($q);
    }else
     $ret.=$callback($q);
   }
  }
  return $ret;
 }
 
 function htmlHex($str){
  return "".$str.";";
 }
 
 function htmlDec($str){
  return "".hexdec($str).";";
 }
 
 function escape($str){
  return hexdec($str) }
 
 function u2utf8($str){
  /******(qiushuiwuhen 2002-8-15)******/
  $sp="!'()*-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~";
  $dec=hexdec($str);
  $bin=decbin($dec);
  $len=strlen($bin);
  $arr=array("c0","e0","f0");
  if($dec>0x7f){
   $ret="";
   for($i=$len,$j=-1;$i>=0;$i-=6,$j++){
    if($i>6)
     $ret="%".dechex(0x80+bindec(substr($bin,$i-6,6))).$ret;
    else
     $ret="%".dechex(hexdec($arr[$j])+bindec(substr($bin,0,6-$i))).$ret;
   }
  }else{
   if(strpos($sp,chr($dec)))
    $ret=chr($dec);
   else
    $ret="%".strtolower($str);
  }
  return $ret;
 }
}

使用范例
$words="中文Abc";
function ex($str){return "[".$str."]";}


$qswh=new qswhGBK("qswhGBK.php");//如果文件名是qswhGBK.php,可省参数

echo("

不带参数:".$qswh->gb2u($words));<br>echo("\n调用内置函数htmlHex:".$qswh->gb2u($words,1));<br>echo("\n调用内置函数htmlDec:".$qswh->gb2u($words,2));<br>echo("\n调用内置函数escape:".$qswh->gb2u($words,3));<br>echo("\n调用内置函数u2utf8:".$qswh->gb2u($words,4));<br>echo("\n调用自定义函数:".$qswh->gb2u($words,ex));<p>效果如下:</p> <p>不带参数:4E2D6587416263调用内置函数htmlHex:中文Abc调用内置函数htmlDec:中文Abc调用内置函数escape:%u4E2D%u6587Abc调用内置函数u2utf8:%e4%b8%ad%e6%96%87Abc调用自定义函数:[4E2D][6587][41][62][63]<br></p>



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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft