php如何进行中文繁简体转换
时间:2021-07-21 来源:互联网
今天PHP爱好者给大家带来很多时候我们需要将中文简体转换成繁体,或是将繁体转换成简体,那么你知道该如何进行转换吗?今天小编要带大家一起了解php进行中文繁简体转换的方法,有需要的小伙伴可以参考参考。希望对大家有所帮助。
<?php
$testC = new TestC ();
$testC->index ();
class TestC {
private $utf8_gb2312;
private $utf8_big5;
public function __construct() {
$this->utf8_gb2312 = "么万与丑专业丛东丝丢两严丧个丬丰临为丽举么义乌乐乔习乡书买乱争于亏云";
$this->utf8_big5 = "麽萬與醜專業叢東絲丟兩嚴喪個爿豐臨為麗舉麼義烏樂喬習鄉書買亂爭於虧雲";
}
public function index() {
// 设置网页UTF8编码
header ( "Content-Type: text/html; charset=utf-8" );
// UTF8内简转繁
$str = "中华人民共和国";
$str_big5 = $this->c2t ( $str );
echo "原文:$str <br />";
echo "转换为繁体后: $str_big5 <br />";
// UTF8内繁转简
$str = "中華人民共和國 ";
$str_gb2312 = $this->t2c ( $str );
echo "原文: $str <br /> ";
echo "转换为简体后:$str_gb2312 <br /> ";
}
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;
}
}
?>
以上就是php如何进行中文繁简体转换的详细内容,更多请关注php爱好者其它相关文章!
-
关于身份证实名认证api接口调用的相关内容 2024-11-25
-
三角洲行动手游hk416武器介绍 2024-11-25
-
归龙潮机关解谜攻略 2024-11-25
-
剑灵2闪避格挡操作技巧 2024-11-25
-
让我们了解下身份证OCR识别api接口调用 2024-11-25
-
球球大作战猎魔逃生玩法攻略 2024-11-25