栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

PHP正则过滤处理微信昵称中emoji字符的方法

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PHP正则过滤处理微信昵称中emoji字符的方法

本文实例讲述了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.jb51.net/regex/javascript

正则表达式在线生成工具:
http://tools.jb51.net/regex/create_reg

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php正则表达式用法总结》、《php程序设计安全教程》、《php安全过滤技巧总结》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《php字符串(string)用法总结》及《php+mysql数据库操作入门教程》

希望本文所述对大家PHP程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/39676.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号