PHP类UTF8编码内的繁简转换-繁体-简体均只⽀持 UTF8 ⽂本,所以如果获得的来源是 $_GET 的话, 就有必要转换⼀下编码了。⽤到的函数是:
$str = $_GET['str'];
// 简体到utf8
$str = iconv("GB2312","utf-8",$str);
// 繁体到utf8
$str = iconv("GBK","utf-8",$str);
第⼀个 见附录:
include "big2gb.php";六小龄童的老婆是谁
$obj=new big2gb;
//简体转换繁体
$string_big5 = $obj->chg_utfcode($string_gb,'big5');
//繁体转换简体
$string_gb = $obj->chg_utfcode($string_big5,'gb2312'');
第⼆个:
<?php
class Trans
{
private $utf8_gb2312;
private $utf8_big5;
public function __construct(){
$this->utf8_gb2312 = "么万与丑专业丛东丝丢两严丧个丬丰临为丽举么义乌乐乔习乡书买乱争于亏云亘亚产亩亲亵亸亿仅从仑仓仪们价众优伙会伛伞伟
$this->utf8_big5 = "麽萬與醜專業叢東絲丟兩嚴喪個⽙豐臨為麗舉麼義烏樂喬習鄉書買亂爭於虧雲亙亞產畝親褻嚲億僅從侖倉儀們價眾優夥會傴傘偉傳 }
public function c2t($str) {
$str_t = '';
$len = strlen($str);
$a = 0;
while ($a < $len){
if (ord($str{$a})>=224 && ord($str{$a})<=239){
if (($temp = strpos( $this->utf8_gb2312, $str{$a} . $str{$a+1} . $str{$a+2})) !== false){
$str_t .= $this->utf8_big5{$temp} . $this->utf8_big5{$temp+1} . $this->utf8_big5{$temp+2};
$a += 3;
continue;
}
}
$str_t .= $str{$a};
$a += 1;沙海沈琼
}
return $str_t;
}翡翠的种类
public function t2c($str) {
$str_t = '';
$len = strlen($str);
$a = 0;
while ($a < $len){
if (ord($str{$a})>=224 && ord($str{$a})<=239){
if (($temp = strpos( $this->utf8_big5, $str{$a} . $str{$a+1} . $str{$a+2})) !== false){
$str_t .= $this->utf8_gb2312{$temp} . $this->utf8_gb2312{$temp+1} . $this->utf8_gb2312{$temp+2};
$a += 3;
continue;
}
}
$str_t .= $str{$a};
$a += 1;
陈龙章龄之婚礼}
return $str_t;
}
}
如何上传视频2、 实例
//本⽂档运⾏于UTF8编码
header("Content-Type: text/html; charset=utf-8");
$go = new Trans;
//UTF8内简转繁
$str = "中华⼈民共和国万岁";
$str_big5 = $go->c2t($str);
echo "原⽂:$str <br />";
echo "转换为繁体后: $str_big5 <br />";
//UTF8内繁转简
$str = "中華⼈民共和國萬歲 ";
$str_gb2312 = $go->t2c($str);
echo "原⽂: $str <br /> ";
echo "转换为简体后:$str_gb2312 <br /> " ;
修改了 繁体中的 彙, 改为 匯
添加么->麽
第三个: mediawiki (优点:可智能辨别词组;缺点:翻译⽹页全页可能会遇到问题)
<?php
/**
* Define the folder(note the ending slash) containing the mediawiki's files.
*
* YOU SHOULD PLACE THEM OUTSIDE THE DOCUMENT ROOT OF YOUR WEB SERVER
*/
define("MEDIAWIKI_PATH", "mediawiki-1.15.2/");
/* Include our helper class */
require_once "mediawiki-zhconverter.inc.php";
/* Convert it, valid variants such as zh, zh-cn, zh-tw, zh-sg & zh-hk */
中国好声音 赵露echo MediaWikiZhConverter::convert("雪糕", "zh-tw") , ",";
echo MediaWikiZhConverter::convert("記憶體", "zh-cn"), ",";
echo MediaWikiZhConverter::convert("⼤卫·贝克汉姆", "zh-hk");
>
第四个: chinese_conversion (优点:可智能辨别词组亦可翻译⽹页;缺点:需要更智能?)
<?
include("convert.php");
function language($str){
return zhconversion_tw($str);//转换为台湾正体
}
ob_start('language');
>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />什么什么什么什么什么
<?
ob_end_flush();
>
个⼈推荐第四个。
发布评论