首页 > 作文

用php实现gb2312和unicode间的编码转换

更新时间:2023-04-07 04:12:24 阅读: 评论:0

gb2312 和 unicode 间的编码转换
下面的例子是将 gb2312 转换为 “全”这种形式
php4.3.1以后的iconv函数很好用的,只是需要自己写一个uft8到unicode的转换函数
查表(gb2312.txt)也行

复制代码 代码如下:

<?

$text = “www.887551.com”;

preg_match_all(“/[\x80-\xff]?./”,$text,$ar);

foreach($ar[0] as $v)

echo “&#”.utf8_unicode(iconv(“gb2312″,”utf-8″,$v)).”;”;

?>

<?

// utf8 -> unicode

function utf8_unicode($c) {

switch(strlen($c)) {

ca 1:

return ord($c);

ca 2:

$n = (ord($c[0]) & 0x3f) << 6;

$n += ord($c[1]) & 0x3f;

return $n;

ca 3:

$n = (ord($c[0]) & 0x1f) << 12;

$n += (ord($c[1]) & 0x3f) << 6;
不完美小孩
$n += ord($c[2]) & 0x3f;

return $n;

ca 4:

$n = (ord($c[0]) & 0x0f) << 18;

$n += (ord($c[1]) & 0x3f) << 12;

$n += (ord($c[2]) & 0x3f) << 6;

$n += ord($c[3]) & 0x3f;

return $n;

}

}

?>

下面的例子是利用php将”全”这中编码转换为gb2312.


复制代码 代码如下:

<?php

$str = “ttl全天候自动聚焦”;

$str = preg_replace(“|&#([0-9]{1,5});|”, “\”.u2utf82gb(\\1).\””, $str);

$str = “\$str=\”$str\”;”;

eval($str);

echo $str;

function u2utf82gb($c){

$str=””;

if ($c < 0x80) {

$str.=$c;

} el if ($c < 0x800) {

$str.=chr(0xc0 | $c>>6);

$str.=chr(0x80 | $c & 0x3f);

} el if ($c < 0x10000) {

$str.=chr(0xe0 | $c>>12);

$str.=chr(0x80 | $c>>6 & 0x3f);

$str.=chr(0x80 | $c & 0x3f);

} el if ($c < 0x200000) {

$str.=chr(0xf0 | $c>>18);

$str.=chr(0x80 | $c>>12 & 0x3f);

$str.=chr(0x80 | $c>>6 & 0x3f);

$str.=chr(0x80 | $c & 0x3f);

}

return iconv(‘utf-8’, ‘gb2312’, $str);

}

?>

或者是


复制代码 代码如下:

function unescape($str) {

$str = rawurldecode($str);

preg_match_all(“/(?:%u.{4})|&#x.{4};|&#\d+;|.+/u”,$str,$r);

$ar = $r[0];

print_r($ar);

foreach($ar as $k=>$v) {

if(substr($v,0,2) == “%u”)

$ar[$k] = iconv(“ucs-2″,”gb2312”,pack(“h4”,substr($v,-4)));

elif(substr($v,0,3) == “&#x”)

$ar[$k] = iconv(“ucs-2″,”gb2312”,pack(“h4”,substr($v,3,-1)));

elif(substr($v,0,2) == “&#”) {

echo substr($v,2,-1).”<br>”;

$ar[$k] = iconv(“ucs-2″,”gb2312”,pack(“n”,substr($v,2,-1)));

}

}

return join(“”,$ar);

}

$str = “ttl全天候自动聚焦”;

echo unescape($str); //out ttl全天候自动聚焦

利用javascript来转换


复制代码 代码如下:

<style>

body {

font-size: 9p描写秋天景色的成语t; padding-right: 0px; padding-left: 0px; padding-bottom: 0px; padding-top: 0px;

}

input {

font-size: 9pt; height: 13pt;

}

</style>

<script language=”javascript1.2″>

/*

this following code are designed and writen by windy_sk <asonx@163.net>

you can u it freely, but u must held all the copyright items!

*/

function str2unicode(str){

var arr = new array();

for(var i=0;i<str.length;i++){

arr[i]=”&#” + str.charcodeat(i) + “;”;

}

return(arr.tostring().repla友情祝福语ce(/,/g,””));

}

function unicode2ostr(str){

var re=/&#[\da-fa-f]{1,5};/ig;

var arr=str.match(re);

if(arr==null)return(“”);

for(var i=0;i<arr.length;i++){

arr[i]=string.fromcharcode(arr[i].replace(/[&#;]/g,””));

}

return(arr.tostring().replace(/,/g,””))

}

function modi_str(){

if(document.all.text.method.checked){

if(document.all.text.decode.value!=””){

document.all.text.encode.value = str2unicode(document.all.text.decode.value);

}el{

document.all.text.decode.value = unicode2ostr(document.all.text.encode.value);

}

}el{

if(document.all.text.encode.value!=””){

document.all.text.decode.value = unicode2ostr(document.all.text.encode.value);

}el{

document.all.text.encode.value = str2unicode(document.all.text.decode.value);

}

}

}

</script>

<title>unicode</title>

<form name=text>

文本原型:<br>

<textarea name=”decode” cols=”100″ rows=”10″></textarea>

<br>

转换代码:<br>

<textarea name=”encode” cols=”100″ rows=”10″></textarea>

<br>

<input type=”checkbox” name=”method” checked> 正向转换

<input type=button onclick=”modi_str()” value=” 确 定 “>

<input type=ret value=” 清 空 “>

<input type=button onclick=”document.all.text.method.checked?document.all.text.encode.lect():document.all.text.decode.lect()” value=” 全 选 “>

</form>

下面是一个显示所有全角半角的字体的查看例子


复制代码 代码如下:

<style>

body {

font-size: 9pt; padding-right: 0px; padding-left: 0px; padding-bottom: 0px; padding-top: 0px;

}

input {

font-size: 9pt; height: 13pt;

}

</style>

<script>

function showuni(min,max){

show.document.open();

show.document.writeln(“<style>body{font-size:9pt;word-break:break-all;}</style>”);

show.document.writeln(min + ” – ” + max + “<br><br>”);

var i=0;

for(i=min;i<=max;i++){

show.document.write(“&#” + i + “;”);

}

show.document.clo();

}

</script>

<input type=button value=”半角” onclick=showuni(32,126)>

<input type=button value=”全角” onclick=showuni(65281,65374)>

<input type=button value=”中文1″ onclick=showuni(19968,40869)>

<input type=button value=”中文2″ onclick=showuni(63744,64045)>

<input type=button value=”日文平” onclick=showuni(12353,1243大灰狼的睡前故事5)>

<input type=button value=”日文片” onclick=showuni(12449,12534)>

<input type=button value=”韩文” onclick=showuni(44032,55203)>

<br>自定义:<input name=min> – <input name=max>

<input type=button value=”察看” onclick=showuni(parint(document.all.min.value),parint(document.all.max.value))>

<br>好开头好结尾

<iframe src=”about:blank” id=show width=100% height=70% scroll=no></iframe>

下面是一个查表(gb2312),转换gb2312到utf8的例子, 现在有iconv函数,这个已经没有太大的意义了,


复制代码 代码如下:

<?

function gb2utf8($gb){

if(!trim($gb)) return $gb;

$filename=”gb2312.txt”;

$tmp=file($filename);

$codetable=array();

while(list($key,$value)=each($tmp))

$codetable[hexdec(substr($value,0,6))]=substr($value,7,6);

$utf8=””;

while($gb) {

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

$this=substr($gb,0,2);

$gb=substr($gb,2,strlen($gb)-2);

$utf8.=u2utf8(hexdec($codetable[hexdec(bin2hex($this))-0x8080]));

}el{

$this=substr($gb,0,1);

$gb=substr($gb,1,strlen($gb)-1);

$utf8.=u2utf8($this);

}

}

return $utf8;

}

function u2utf8($c){

$str=””;

if ($c < 0x80) {

$str.=$c;

} el if ($c < 0x800) {

$str.=chr(0xc0 | $c>>6);

$str.=chr(0x80 | $c & 0x3f);

} el if ($c < 0x10000) {

$str.=chr(0xe0 | $c>>12);

$str.=chr(0x80 | $c>>6 & 0x3f);

$str.=chr(0x80 | $c & 0x3f);

} el if ($c < 0x200000) {

$str.=chr(0xf0 | $c>>18);

$str.=chr(0x80 | $c>>12 & 0x3f);

$str.=chr(0x80 | $c>>6 & 0x3f);

$str.=chr(0x80 | $c & 0x3f);

}

return $str;

}

?>

本文发布于:2023-04-07 04:12:23,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/7928e303e0c094ae374de8825122b7c2.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:用php实现gb2312和unicode间的编码转换.doc

本文 PDF 下载地址:用php实现gb2312和unicode间的编码转换.pdf

标签:代码   全角   是一个   例子
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图