zoukankan      html  css  js  c++  java
  • MYSQL 写入emoji表情字符处理

    这个鬼emoji表情是4个字节,mysql使用的utf8编码,UTF8占3个字节,要存储那个emoji表情需要将mysql编码由UFT8改为UFT8的超集,utf8mb4;

    改数据库编码容易引起大面的乱码灾难。所以当遇到emoji字符表情的时候做特殊处理。网上也有很多处理方案,最后找到了一个贴上地址和代码:https://github.com/BriquzStudio/php-emoji ,多谢

    class Emoji
    {
    	/**
    	 * Encode emoji in text
    	 * @param string $text text to encode
    	 */
    	public static function Encode($text) {
    		return self::convertEmoji($text,"ENCODE");
    	}
    	/**
    	 * Decode emoji in text
    	 * @param string $text text to decode
    	 */
    	public static function Decode($text) {
    		return self::convertEmoji($text,"DECODE");
    	}
    	private static function convertEmoji($text,$op) {
    		if($op=="ENCODE"){
    			return preg_replace_callback('/([0-9|#][x{20E3}])|[x{00ae}|x{00a9}|x{203C}|x{2047}|x{2048}|x{2049}|x{3030}|x{303D}|x{2139}|x{2122}|x{3297}|x{3299}][x{FE00}-x{FEFF}]?|[x{2190}-x{21FF}][x{FE00}-x{FEFF}]?|[x{2300}-x{23FF}][x{FE00}-x{FEFF}]?|[x{2460}-x{24FF}][x{FE00}-x{FEFF}]?|[x{25A0}-x{25FF}][x{FE00}-x{FEFF}]?|[x{2600}-x{27BF}][x{FE00}-x{FEFF}]?|[x{2600}-x{27BF}][x{1F000}-x{1FEFF}]?|[x{2900}-x{297F}][x{FE00}-x{FEFF}]?|[x{2B00}-x{2BF0}][x{FE00}-x{FEFF}]?|[x{1F000}-x{1F9FF}][x{FE00}-x{FEFF}]?|[x{1F000}-x{1F9FF}][x{1F000}-x{1FEFF}]?/u',array('self',"encodeEmoji"),$text);
    		}else{
    			return preg_replace_callback('/(\u[0-9a-f]{4})+/',array('self',"decodeEmoji"),$text);
    		}
    	}
    	private static function encodeEmoji($match) {
    		return str_replace(array('[',']','"'),'',json_encode($match));
    	}
    	
    	private static function decodeEmoji($text) {
    		if(!$text) return '';
    		$text = $text[0];
    		$decode = json_decode($text,true);
    		if($decode) return $decode;
    		$text = '["' . $text . '"]';
    		$decode = json_decode($text);
    		if(count($decode) == 1){
    		   return $decode[0];
    		}
    		return $text;
    	}
    }
    

     

    $nickName = Emoji::Decode($userinfo['nickname']);

    $realName = empty($nickName) ? '微信用户:' . time() : $nickName;

  • 相关阅读:
    linux 配置Apache 、PHP
    SQL Server DML(SELECT)常见用法(二)
    SQL Server DML(UPDATE、INSERT、DELETE)常见用法(一)
    PropertyGrid—添加EventTab
    PropertyGrid—添加属性Tab
    PropertyGrid—默认属性,默认事件,属性默认值
    PropertyGrid—为复杂属性提供下拉式编辑框和弹出式编辑框
    PropertyGrid--为复杂属性提供编辑功能
    PropertyGrid--基本功能
    Intellij IDEA使用(一)项目模板类型
  • 原文地址:https://www.cnblogs.com/Tsai_Quinn/p/5811364.html
Copyright © 2011-2022 走看看