Emoji.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\utils;
  8. /**
  9. * @property Store $store
  10. */
  11. class Emoji
  12. {
  13. public static function emoji($input)
  14. {
  15. $output = "";
  16. if(strpos($input,'[[EMOJI:') !== false)
  17. {
  18. $output = Emoji::emoji_decode($input);
  19. }
  20. else
  21. {
  22. $output = Emoji::emoji_encode($input);
  23. }
  24. return $output;
  25. }
  26. //对emoji表情转义
  27. public static function emoji_encode($str)
  28. {
  29. $strEncode = '';
  30. $length = mb_strlen($str,'utf-8');
  31. for ($i=0; $i < $length; $i++)
  32. {
  33. $_tmpStr = mb_substr($str,$i,1,'utf-8');
  34. if (strlen($_tmpStr) >= 4)
  35. {
  36. $strEncode .= '[[EMOJI:'.rawurlencode($_tmpStr).']]';
  37. }
  38. else
  39. {
  40. $strEncode .= $_tmpStr;
  41. }
  42. }
  43. return $strEncode;
  44. }
  45. //对emoji表情转反义
  46. public static function emoji_decode($str)
  47. {
  48. $strDecode = preg_replace_callback('|\[\[EMOJI:(.*?)\]\]|', function($matches){
  49. return rawurldecode($matches[1]);
  50. }, $str);
  51. return $strDecode;
  52. }
  53. }