PHP 转义emoji表情

85 阅读1分钟

我正在参加「掘金·启航计划」

image.png

  • 转码
function userTextEncode($str){
	if(!is_string($str)) return $str;
	if(!$str || $str=='undefined') return '';
	$text = json_encode($str);
	$text = preg_replace_callback("/(\\\u[ed][0-9a-f]{3})/i",function($str){
		return addslashes($str[0]);
	},$text);
	return json_decode($text);
}
  • 解码
function userTextDecode($str){
	$text = json_encode($str);
	$text = preg_replace_callback('/\\\\\\\\/i',function($str){
		return '\\';
	},$text);
	return json_decode($text);
}