轉換unicode十進制內碼爲utf-8編碼ide
- function u2utf8($c)
- {
- $str = "";
- if ($c < 0x80) {
- $str .= $c;
- } else if ($c < 0x800) {
- $str .= chr(0xC0 | $c >> 6);
- $str .= chr(0x80 | $c & 0x3F);
- } else if ($c < 0x10000) {
- $str .= chr(0xE0 | $c >> 12);
- $str .= chr(0x80 | $c >> 6 & 0x3F);
- $str .= chr(0x80 | $c & 0x3F);
- } else if ($c < 0x200000) {
- $str .= chr(0xF0 | $c >> 18);
- $str .= chr(0x80 | $c >> 12 & 0x3F);
- $str .= chr(0x80 | $c >> 6 & 0x3F);
- $str .= chr(0x80 | $c & 0x3F);
- }
- return $str;
- }
- $source = "中国人民";
- preg_match_all("/&#([0-9]+)/", $source, $regs);
- foreach ($regs[1] as $v) {
- $source = str_replace("&#$v", u2utf8($v), $source);
- }
- echo $source;
- // 中國人民 //