微信获取用户信息包含表情符号导致写入MySQL数据库错误

方案一:修改数据库为支持4个字节的utf8mb4编码,原因请看这里

1.升级MySQL到支持uft8mb4编码的最低版本5.5以上(我开发环境本身已经支持)

2.更改MySQL设置,修改/etc/my.cnf配置文件

在[client]后追加或修改default-character-set = utf8mb4

在[mysqld]后追加或修改character_set_server = utf8mb4

方案二:将获取的微信昵称等进行urlencode转码之后再存储到数据库中,显示的时候urldecode

方案三:将获取的微信昵称等中特殊字符过滤(明显下下策,只是一种可能性)

  1. public static function removeEmoji($text) {
  2. $clean_text = "";
  3. // Match Emoticons
  4. $regexEmoticons = '/[\x{1F600}-\x{1F64F}]/u';
  5. $clean_text = preg_replace($regexEmoticons, '', $text);
  6. // Match Miscellaneous Symbols and Pictographs
  7. $regexSymbols = '/[\x{1F300}-\x{1F5FF}]/u';
  8. $clean_text = preg_replace($regexSymbols, '', $clean_text);
  9. // Match Transport And Map Symbols
  10. $regexTransport = '/[\x{1F680}-\x{1F6FF}]/u';
  11. $clean_text = preg_replace($regexTransport, '', $clean_text);
  12. // Match Miscellaneous Symbols
  13. $regexMisc = '/[\x{2600}-\x{26FF}]/u';
  14. $clean_text = preg_replace($regexMisc, '', $clean_text);
  15. // Match Dingbats
  16. $regexDingbats = '/[\x{2700}-\x{27BF}]/u';
  17. $clean_text = preg_replace($regexDingbats, '', $clean_text);
  18. return $clean_text;
  19. }

最终我使用了方案二,没毛病,就是稍微烦点,搜索条件啥的记得转码就是了。


data.update.lastsys.time.agopunc.commaarticle.create.bypunc.commaarticle.file.inpunc.colonMySQL
Powered By Valine
v1.5.2