Removing emojis from variable

后端 未结 3 1206
天命终不由人
天命终不由人 2021-02-04 20:36

I\'m using Smarty to pass in and display the contents of a first_name variable. Some users have Emoji characters (http://en.wikipedia.org/wiki/Emoji) in their

相关标签:
3条回答
  • 2021-02-04 21:16

    I tried some of the solutions posted above, but no one worked, however, when I converted the string to UTF-8 using the mb_ function it works properly.

    You can use:

    trim( preg_replace('/[\x00-\x1F\x80-\xFF]/', '', 
          mb_convert_encoding( $emojiString, "UTF-8" ) ) );
    

    Works for me.

    0 讨论(0)
  • 2021-02-04 21:20

    The emoji are encoded in the block U+1F300–U+1F5FF.

    preg_replace('/\xEE[\x80-\xBF][\x80-\xBF]|\xEF[\x81-\x83][\x80-\xBF]/', '', $first_name)
    

    this will strip those out

    0 讨论(0)
  • 2021-02-04 21:26

    Q: Can this be done with Smarty? A: Yes.

    Q: Can it be done with PHP in Smarty? A: Yes. But please don't use PHP tags on template side.

    Try to use a variable modifier on a template variable instead.

    {* apply modifier to a variable *}
    {$first_name|emojistrip}
    

    Put the following content into a file named "modifier.emojistrip.php" in the folder "/smarty/plugins/".

    function smarty_modifier_emojistrip($string)
    {       
        return preg_replace('/\xEE[\x80-\xBF][\x80-\xBF]|\xEF[\x81-\x83][\x80-\xBF]/', '', $string);
    }
    

    0 讨论(0)
提交回复
热议问题