本文实例讲述了PHP正则过滤处理微信昵称中emoji字符的方法。分享给大家供大家参考,具体如下:
今天刚做了一个微信应用,在获取微信昵称的过程中报错了,经查原因是微信昵称中包含emoji字符,在写入数据库的时候出错,所以想办法在写入之前把这些字符过滤掉,于是在网上找到一个方法,记录一下。
移除微信昵称中的emoji字符:
function removeEmoji($nickname) {
$clean_text = "";
// Match Emoticons
$regexEmoticons = '/[x{1F600}-x{1F64F}]/u';
$clean_text = preg_replace($regexEmoticons, '', $text);
// Match Miscellaneous Symbols and Pictographs
$regexSymbols = '/[x{1F300}-x{1F5FF}]/u';
$clean_text = preg_replace($regexSymbols, '', $clean_text);
// Match Transport And Map Symbols
$regexTransport = '/[x{1F680}-x{1F6FF}]/u';
$clean_text = preg_replace($regexTransport, '', $clean_text);
// Match Miscellaneous Symbols
$regexMisc = '/[x{2600}-x{26FF}]/u';
$clean_text = preg_replace($regexMisc, '', $clean_text);
// Match Dingbats
$regexDingbats = '/[x{2700}-x{27BF}]/u';
$clean_text = preg_replace($regexDingbats, '', $clean_text);
return $clean_text;
}
另外还发现一个github开源应用,还没有研究测试。
https://github.com/iamcal/php-emoji
补充:今天又在网上找到一个更简单的方法
// 过滤掉emoji表情
function filterEmoji($str)
{
$str = preg_replace_callback( '/./u',
function (array $match) {
return strlen($match[0]) >= 4 ? '' : $match[0];
},
$str);
return $str;
}
PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:
JavaScript正则表达式在线测试工具:
http://tools.uoften.com/regex/javascript
正则表达式在线生成工具:
http://tools.uoften.com/regex/create_reg
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php正则表达式用法总结》、《php程序设计安全教程》、《php安全过滤技巧总结》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《php字符串(string)用法总结》及《php+mysql数据库操作入门教程》
希望本文所述对大家PHP程序设计有所帮助。
最后
以上就是仁爱烤鸡最近收集整理的关于PHP正则过滤处理微信昵称中emoji字符的方法的全部内容,更多相关PHP正则过滤处理微信昵称中emoji字符内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复