1

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

e.g. (買嘢) convert to (&#36023 ;&#22050 ;)
Matt
  • 14,906
  • 27
  • 99
  • 149
Paengski
  • 339
  • 2
  • 6
  • 15

1 Answers1

5
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

Parfait
  • 1,752
  • 1
  • 11
  • 12
  • I'm trying to figure out how to do the opposite of this. (I want Unicode to Chinese characters.) Do you know how? Thanks. – Ryan Oct 22 '14 at 19:14
  • Ryan, function of unicode to chinese is ready, please check again – Parfait Oct 23 '14 at 04:46