`

[转]汉字 與 UNICODE 互相轉換

 
阅读更多
$str = '我';

/* 將 '我' 轉換成 '25105' 或 '我' */
// 使用 iconv
$unicode_html = base_convert(bin2hex(iconv('UTF-8', 'UCS-4', $str)), 16, 10); // 25105

// 使用 mb_convert_encoding
$unicode_html = base_convert(bin2hex(mb_convert_encoding($str, 'ucs-4', 'utf-8')), 16, 10); // 25105

// 補上 &#xxxxx;
$unicode_html = '&#' . base_convert(bin2hex(iconv("utf-8", "ucs-4", $str)), 16, 10) . ';'; // 我

// 將 &#25105 轉回 '我'
$str = mb_convert_encoding($unicode_html, 'UTF-8', 'HTML-ENTITIES'); // '我', $unicode_html = '&#25105'
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics