当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP正则过滤处理微信昵称中emoji字符的方法

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

2018年08月08日  | 移动技术网IT编程  | 我要评论

拳击黑市,大雪纷飞造句,iu好日子歌词

本文实例讲述了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开源应用,还没有研究测试。

补充:今天又在网上找到一个更简单的方法

// 过滤掉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正则表达式在线测试工具:

正则表达式在线生成工具:

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

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网