php教程 php手册 用PHP实现将GB编码转换为UTF8

用PHP实现将GB编码转换为UTF8

Jun 13, 2016 pm 12:36 PM
php utf8 ~을 위한 암호 복사 문서 사용 코딩 전환하다

gb2utf8.php 文件如下:

复制代码 代码如下:


Class GB2UTF8 

var $gb; // 待转换的GB2312字符串 
var $utf8; // 转换后的UTF8字符串 
var $CodeTable; // 转换过程中使用的GB2312代码文件数组 
var $ErrorMsg; // 转换过程之中的错误讯息 

function GB2UTF8($InStr="") 

$this->gb=$InStr; 
$this->SetGb2312(); 
($this->gb=="")?0:$this->Convert(); 


function SetGb2312($InStr="gb2312.txt") 
{ // 设置gb2312代码文件,默认为gb2312.txt 
$this->ErrorMsg=""; 
$tmp=@file($InStr); 
 if (!$tmp) { 
 $this->ErrorMsg="No GB2312"; 
 return false; 
 } 
$this->CodeTable=array(); 
while(list($key,$value)=each($tmp)) { 
$this->CodeTable[hexdec(substr($value,0,6))]=substr($value,7,6); 



function Convert() 
{ // 转换GB2312字符串到UTF8字符串,需预先设置$gb 
$this->utf8=""; 
if(!trim($this->gb) || $this->ErrorMsg!="") { 
return ($this->utf8=$this->ErrorMsg); 

 $str=$this->gb; 

while($str) { 
if (ord(substr($str,0,1))>127) 

$tmp=substr($str,0,2); 
$str=substr($str,2,strlen($str)); 
$tmp=$this->U2UTF8(hexdec($this->CodeTable[hexdec(bin2hex($tmp))-0x8080])); 
for($i=0;$i$this->utf8.=chr(substr($tmp,$i,3)); 

else 

$tmp=substr($str,0,1); 
$str=substr($str,1,strlen($str)); 
$this->utf8.=$tmp; 


return $this->utf8; 


function U2UTF8($InStr) 

for($i=0;$i$str=""; 
if ($InStr $str.=ord($InStr); 

else if ($InStr $str.=(0xC0 | $InStr>>6); 
$str.=(0x80 | $InStr & 0x3F); 

else if ($InStr $str.=(0xE0 | $InStr>>12); 
$str.=(0x80 | $InStr>>6 & 0x3F); 
$str.=(0x80 | $InStr & 0x3F); 

else if ($InStr $str.=(0xF0 | $InStr>>18); 
$str.=(0x80 | $InStr>>12 & 0x3F); 
$str.=(0x80 | $InStr>>6 & 0x3F); 
$str.=(0x80 | $InStr & 0x3F); 

return $str; 

}
?>


测试文件如下:

复制代码 代码如下:


Header("Content-type: image/png"); 
$im = imagecreate(400,300); 
$black = ImageColorAllocate($im, 0,0,0); 
$white = ImageColorAllocate($im, 184,44,6); 
include("gb2utf8.php"); 
$obj=new gb2utf8(); 
$obj->gb="123abc中国456def测试正确"; 
$obj->Convert(); 
ImageTTFText($im, 20, 0, 5, 50, $white, "SIMKAI.TTF", $obj->utf8); 
ImagePNG($im); 
ImageDestroy($im);
?>


说明: 
需要正确设置font文件,请先确认可以使用font直接(不使用gb2utf8)输出英文。 
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

뜨거운 기사 태그

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드 Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드 Dec 24, 2024 pm 04:42 PM

Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드

CakePHP 날짜 및 시간 CakePHP 날짜 및 시간 Sep 10, 2024 pm 05:27 PM

CakePHP 날짜 및 시간

CakePHP 프로젝트 구성 CakePHP 프로젝트 구성 Sep 10, 2024 pm 05:25 PM

CakePHP 프로젝트 구성

CakePHP 파일 업로드 CakePHP 파일 업로드 Sep 10, 2024 pm 05:27 PM

CakePHP 파일 업로드

CakePHP 라우팅 CakePHP 라우팅 Sep 10, 2024 pm 05:25 PM

CakePHP 라우팅

CakePHP 토론 CakePHP 토론 Sep 10, 2024 pm 05:28 PM

CakePHP 토론

CakePHP 빠른 가이드 CakePHP 빠른 가이드 Sep 10, 2024 pm 05:27 PM

CakePHP 빠른 가이드

PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법 PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법 Dec 20, 2024 am 11:31 AM

PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법

See all articles