標籤:php emoji html字元實體
移動端輸入時允許輸入表情,表情實際上是UTF-16編碼,在資料庫儲存上會存在丟失,當前如果把資料庫字碼編碼修改為 utf8mb4 也是可以儲存。
如果不想修改其它則可以通過轉換把這些Emoji轉換為HTML字元實體儲存。
代碼如下:
function utf16_to_entities(){ $content = mb_convert_encoding($content, ‘utf-16‘); $bin = bin2hex($content); $arr = str_split($bin, 4); $l = count($arr); $str = ‘‘; for ($n = 0; $n < $l; $n++) { if (isset($arr[$n + 1]) && (‘0x‘ . $arr[$n] >= 0xd800 && ‘0x‘ . $arr[$n] <= 0xdbff && ‘0x‘ . $arr[$n + 1] >= 0xdc00 && ‘0x‘ . $arr[$n + 1] <= 0xdfff)) { $H = ‘0x‘ . $arr[$n]; $L = ‘0x‘ . $arr[$n + 1]; $code = ($H - 0xD800) * 0x400 + 0x10000 + $L - 0xDC00; $str.= ‘&#‘ . $code . ‘;‘; $n++; } else { $str.=mb_convert_encoding(hex2bin($arr[$n]),‘utf-8‘,‘utf-16‘); } } return $str;}
注意:這裡字元儲存為 utf-8 如果代碼所處理的格式為 GBK 自行修改。
本文出自 “秋風掃落葉” 部落格,請務必保留此出處http://php2012web.blog.51cto.com/5585213/1863313
PHP轉換emoji表情為HTML字元實體