這篇文章主要介紹“php怎么進行中文繁簡體轉換”,在日常操作中,相信很多人在php怎么進行中文繁簡體轉換問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”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怎么進行中文繁簡體轉換”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。