Paengski
Paengski

Reputation: 349

Convert Chinese character to Unicode using PHP

Is there a way in PHP to convert Chinese characters to Unicode?

e.g. (買嘢) convert to (&#36023 ;&#22050 ;)

Upvotes: 2

Views: 7403

Answers (1)

Parfait
Parfait

Reputation: 1750

function chineseToUnicode($str){
    //split word
    preg_match_all('/./u',$str,$matches);

    $c = "";
    foreach($matches[0] as $m){
            $c .= "&#".base_convert(bin2hex(iconv('UTF-8',"UCS-4",$m)),16,10);
    }
    return $c;
}
//Print result
$str = "雨傘運動或稱雨傘革命、遮打運動、遮打革命(英語:Umbrella Movement 或 Umbrella Revolution)是於2014年9月26日起在香港為爭取真普選而發起的一系列公民抗命,逾60-70萬(人次)香港市民佔據多個主要商業區靜坐及示威,地點包括金鐘、中環、灣仔、銅鑼灣、旺角和尖沙咀,旨在要求包括撤回中國全國人大常委會(人大常委)所確定之2017年行政長官選舉及2016年立法會選舉框架和候選人提名方案,爭取行政長官選舉的公民提名權及取消立法會功能界別等訴求。";
echo chineseToUnicode($str);

http://pastebin.com/zZfci0Dz

For Ryan (utf8 to unicode + unicode to utf8)

Full Example Code in PHP

http://ideone.com/td1TUv

Upvotes: 5

Related Questions