移動端輸入時容許輸入表情,表情其實是UTF-16編碼,在數據庫存儲上會存在丟失,當前若是把數據庫字碼編碼修改成 utf8mb4 也是能夠保存。php
若是不想修改其它則能夠經過轉換把這些表情符轉換爲HTML字符實體保存。數據庫
代碼以下:
ide
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 自行修改。
編碼