瞧一瞧看一看

[PHP][微信开发]解决微信昵称中自带表情与手机emoji表情存取

PHP 田丰硕 6987℃ 0评论

还不是太完善  但是已经可以兼容 90%的表情了.

需要使用到php-emoji 中的 文件  emoji.php  ,emoji.png , emoji.css

php-emoji 下载请百度

最后需要的文件数

QQ图片20160202114903

下面是替换标准emoji表情的Emoji.class.php

<?php 
//微信Emoji处理文件
include_once "Face.class.php";
//市面通用Emoji处理
include_once "emoji.php";
//微信与ios与安卓的Emoji的整合处理
class Emoji{
 /**
 * 
 * @param string $text
 * @param string $isDecode 是否进行json_decode
 */
 public static function unified_to_html($text,$isDecode = true){
 if ( ! $text) {
 return $text;
 }
 $text = $isDecode ? json_decode($text) : $text;
 $text = Face::replaceHtml($text);
 $text = Face::reverseImg($text);
 $text = emoji_softbank_to_unified($text);
 $text = emoji_unified_to_html($text);
 $text = json_encode($text);
 $text = preg_replace_callback("#\\\uf([0-9a-f]{3})+#iUs", function($r) { return '';}, $text);
 $text = json_decode($text);
 return $text;
 } 
}
?>

下面是 替换微信内置表情的类Face.class.php

<?php

/**
 * 表情类
 */
class Face{
 public static $face_puth = '<img src="http://cache.soso.com/img/img/e%s.gif" width="20px" height="20px">';
 public static $_faces = array(
 '/::)'=>'[微笑]',
 '/::~'=>'[撇嘴]',
 '/::B'=>'[色]',
 '/::|'=>'[发呆]',
 '/:8-)'=>'[得意]',
 '/::<'=>'[流泪]',
 '/::$'=>'[害羞]',
 '/::X'=>'[闭嘴]',
 '/::Z'=>'[睡]',
 '/::\'('=>'[大哭]',
 '/::-|'=>'[尴尬]',
 '/::@'=>'[发怒]',
 '/::P'=>'[调皮]',
 '/::D'=>'[呲牙]',
 '/::O'=>'[惊讶]',
 ' /::('=>'[难过]',
 '/::+'=>'[酷]',
 '/:--b'=>'[冷汗]',
 '/::Q'=>'[抓狂]',
 '/::T'=>'[吐]',
 '/:,@P'=>'[偷笑]',
 '/:,@-D'=>'[愉快]',
 '/::d'=>'[白眼]',
 '/:,@o'=>'[傲慢]',
 '/::g'=>'[饥饿]',
 '/:|-)'=>'[困]',
 '/::!'=>'[惊恐]',
 '/::L'=>'[流汗]',
 '/::>'=>'[憨笑]',
 '/::,@'=>'[悠闲]',
 '/:,@f'=>'[奋斗]',
 '/::-S'=>'[咒骂]',
 '/:?' =>'[疑问]',
 '/:,@x'=>'[嘘]',
 '/:,@@'=>'[晕]',
 '/::8'=>'[疯了]',
 '/:,@!'=>'[衰]',
 '/:!!!'=>'[骷髅]',
 '/:xx'=>'[敲打]',
 '/:bye'=>'[再见]',
 '/:wipe'=>'[擦汗]',
 '/:dig'=>'[抠鼻]',
 '/:handclap'=>'[鼓掌]',
 '/:&-('=>'[糗大了]',
 '/:B-)'=>'[坏笑]',
 '/:<@'=>'[左哼哼]',
 '/:@>'=>'[右哼哼]',
 '/::-O'=>'[哈欠]',
 '/:>-|'=>'[鄙视]',
 '/:P-('=>'[委屈]',
 '/::\'|'=>'[快哭了]',
 '/:X-)'=>'[阴险]',
 '/::*'=>'[亲亲]',
 '/:@x'=>'[吓]',
 '/:8*'=>'[可怜]',
 '/:pd'=>'[菜刀]',
 '/:<W>'=>'[西瓜]',
 '/:beer'=>'[啤酒]',
 '/:basketb'=>'[篮球]',
 '/:oo'=>'[乒乓]',
 '/:coffee'=>'[咖啡]',
 '/:eat'=>'[饭]',
 '/:pig'=>'[猪头]',
 '/:rose'=>'[玫瑰]',
 '/:fade'=>'[凋谢]',
 '/:fade'=>'[凋谢]',
 '/:showlove'=>'[嘴唇]',
 '/:heart'=>'[爱心]',
 '/:break'=>'[心碎]',
 '/:cake'=>'[蛋糕]',
 '/:li'=>'[闪电]',
 '/:bome'=>'[炸弹]',
 '/:kn'=>'[刀]',
 '/:footb'=>'[足球]',
 '/:ladybug'=>'[瓢虫]',
 '/:shit'=>'[便便]',
 '/:moon'=>'[月亮]',
 '/:sun'=>'[太阳]',
 '/:gift'=>'[礼物]',
 '/:hug'=>'[拥抱]',
 '/:strong'=>'[强]',
 '/:weak'=>'[弱]',
 '/:share'=>'[握手]',
 '/:v'=>'[胜利]',
 '/:@)'=>'[抱拳]',
 '/:jj'=>'[勾引]',
 '/:@@'=>'[拳头]',
 '/:bad'=>'[差劲]',
 '/:lvu'=>'[爱你]',
 '/:no'=>'[NO]',
 '/:ok'=>'[OK]',
 '/:love'=>'[爱情]',
 '/:<L>'=>'[飞吻]',
 '/:jump'=>'[跳跳]',
 '/:shake'=>'[发抖]',
 '/:<O>' =>'[怄火]',
 '/:circle'=>'[转圈]',
 '/:kotow'=>'[磕头]',
 '/:turn'=>'[回头]',
 '/:skip'=>'[跳绳]',
 '/:oY'=>'[投降]',
 '/:#-0'=>'[激动]',
 '/:hiphot'=>'[乱舞]',
 '/:kiss'=>'[献吻]',
 '/:<&'=>'[左太极]',
 '/:&>'=>'[右太极]'
 );

 public static $wx_face = array (
 '/::)' => 100, //微笑
 '/::~' => 101, //撇嘴 
 '/::B' => 102, //色
 '/::|' => 103, //发呆
 '/:8-)' => 104, //得意
 '/::<' => 105, //流泪
 '/::$' => 106, //害羞
 '/::X' => 107, //闭嘴
 '/::Z' => 108, //睡
 '/::\'(' => 109, //大哭
 '/::-|' => 110, //尴尬
 '/::@' => 111, //发怒
 '/::P' => 112, //调皮
 '/::D' => 113, //呲牙
 '/::O' => 114, //惊讶
 ' /::(' => 115, //难过
 '/::+' => 116, //酷
 '/:--b' => 117, //冷汗
 '/::Q' => 118, //抓狂
 '/::T' => 119, //吐
 '/:,@P' => 120, //偷笑
 '/:,@-D' => 121, //愉快
 '/::d' => 122, //白眼
 '/:,@o' => 123, //傲慢
 '/::g' => 124, //饥饿
 '/:|-)' => 125, //困
 '/::!' => 126, //惊恐
 '/::L' => 127, //流汗
 '/::>' => 128, //憨笑
 '/::,@' => 129, //悠闲
 '/:,@f' => 130, //奋斗
 '/::-S' => 131, //咒骂
 '/:?' => 132, //疑问
 '/:,@x' => 133, //嘘
 '/:,@@' => 134, //晕
 '/::8' => 135, //疯了
 '/:,@!' => 136, //衰
 '/:!!!' => 137, //骷髅
 '/:xx' => 138, //敲打
 '/:bye' => 139, //再见
 '/:wipe' => 140, //擦汗
 '/:dig' => 141, //抠鼻
 '/:handclap' => 142, //鼓掌
 '/:&-(' => 143, //糗大了
 '/:B-)' => 144, //坏笑
 '/:<@' => 145, //左哼哼
 

 '/:@>' => 146, //右哼哼
 '/::-O' => 147, //哈欠
 '/:>-|' => 148, //鄙视
 '/:P-(' => 149, //委屈
 '/::\'|' => 150, //快哭了
 '/:X-)' => 151, //阴险
 '/::*' => 152, //亲亲
 '/:@x' => 153, //吓
 '/:8*' => 154, //可怜
 '/:pd' => 155, //菜刀
 '/:<W>' => 156, //西瓜
 '/:beer' => 157, //啤酒
 '/:basketb' => 158, //篮球
 '/:oo' => 159, //乒乓
 '/:coffee' => 160, //咖啡
 '/:eat' => 161, //饭
 '/:pig' => 162, //猪头
 '/:rose' => 163, //玫瑰
 '/:fade' => 164, //凋谢
 '/:fade' => 164, //凋谢
 '/:showlove' => 165, //嘴唇
 '/:heart' => 166, //爱心
 '/:break' => 167, //心碎
 '/:cake' => 168, //蛋糕
 

 '/:li' => 169, //闪电
 '/:bome' => 170, //炸弹
 '/:kn' => 171, //刀
 '/:footb' => 172, //足球
 '/:ladybug' => 173, //瓢虫
 '/:shit' => 174, //便便
 '/:moon' => 175, //月亮
 '/:sun' => 176, //太阳
 '/:gift' => 177, //礼物
 '/:hug' => 178, //拥抱
 '/:strong' => 179, //强
 '/:weak' => 180, //弱
 '/:share' => 181, //握手
 '/:v' => 182, //胜利
 '/:@)' => 183, //抱拳
 '/:jj' => 184, //勾引
 '/:@@' => 185, //拳头
 '/:bad' => 186, //差劲
 '/:lvu' => 187, //爱你
 '/:no' => 188, //NO
 '/:ok' => 189, //OK
 '/:love' => 190, //爱情
 '/:<L>' => 191, //飞吻
 
 '/:jump' => 192, //跳跳
 '/:shake' => 193, //发抖
 '/:<O>' => 194, //怄火
 '/:circle' => 195, //转圈
 '/:kotow' => 196, //磕头
 '/:turn' => 197, //回头
 '/:skip' => 198, //跳绳
 '/:oY' => 199, //投降
 '/:#-0' => 200, //激动
 '/:hiphot' => 201, //乱舞
 '/:kiss' => 202, //献吻
 '/:<&' => 203, //左太极
 '/:&>' => 204 //右太极
 );

 static function replaceHtml($content){
 self::$_faces=array_flip(self::$_faces);
 $key=array_keys(self::$_faces);
 $value=array_values(self::$_faces);
 $content=str_replace($key,$value,$content);
 return $content;
 
 }

 static function reverseHtml($content){
 $content=str_replace(array("<br>","<br>","&nbsp;"),array("\n","\r"," "),$content);
 $key=array_keys(self::$_faces);
 $value=array_values(self::$_faces);
 $content=str_replace($key,$value,$content);
 return $content;
 }

 static function reverseImg($content){
 $content=str_replace(array("\n","\r"," "),array("<br>","<br>","&nbsp;"),$content);
 $arr=array();
 foreach(self::$wx_face as $k=>$v){
 $arr[$k]=sprintf(self::$face_puth, $v);
 }

 $face_keys = array_keys($arr);
 $face_values = array_values($arr);
 return str_replace($face_keys,$face_values,$content);
 }
}

下面是测试使用文件ceshi.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
 <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
 <title>Emoji HTML Test</title>
 <link href="emoji.css" rel="stylesheet" type="text/css" />
</head>
<body>

</body>
</html>
<?php
include 'Emoji.class.php';
function http_get($url){
 $oCurl = curl_init();
 if(stripos($url,"https://")!==FALSE){
 curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
 curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
 curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
 }
 curl_setopt($oCurl, CURLOPT_URL, $url);
 curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
 $sContent = curl_exec($oCurl);
 $aStatus = curl_getinfo($oCurl);
 curl_close($oCurl);
 if(intval($aStatus["http_code"])==200){
 return $sContent;
 }else{
 return false;
 }
}
$openid = "qqqqqqqqqqq"; //****请替换
//http://www.vdogo.com/mayi/c/cs.php
$accessToken = "xxxxxxxxxxxxx";//****请替换
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$accessToken&openid=$openid&lang=zh_CN";
$info = http_get($url);
$info = json_decode($info,true);
$text = json_encode($info['nickname']);
//---------------------以上为获取测试数据----------------------
/*
 * 数据如果入库请将其json_encode取出后在进行解密替换 
 * 使用说明需先引入Emoji.class.php 文件
 * 引入<link href="emoji.css" rel="stylesheet" type="text/css" /> css 文件
 */
//使用 $Text为json_encode 后的数据
//如为直接获取的微信的昵称 可直接进行参数的传递 ,第二个参数填写 false 不进行json_decode 默认为TRUE
echo Emoji::unified_to_html($text,true);

?>

转载请注明:田丰硕个人博客 » [PHP][微信开发]解决微信昵称中自带表情与手机emoji表情存取

喜欢 (5)
发表我的评论
取消评论
表情
(1)个小伙伴在吐槽
  1. 票据宝理财产品年化收益率最高15%,100%银行承兑
    模特2016-08-18 16:26 回复