Removing emojis from variable
Asked Answered
G

4

6

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 first_name and I am wondering how I can either a) conditionally not display a user's first_name if it contains emojis or b) filter out emoji characters from first_name. Can this be done with Smarty? Can it be done with PHP in Smarty?

Glimmer answered 30/10, 2012 at 22:46 Comment(1)
IT is just (correct me if im wrong) U+F000 to U+FFFFTypescript
M
15

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

Melnick answered 30/10, 2012 at 23:16 Comment(5)
I tried this with no luck, but I'm not sure if I am implementing correctly. Here's how I tried coding it: jsbin.com/epefej/1/edit.Glimmer
@585connor: You need to do something like $first_name = preg_replace('~\xEE[\x80-\xBF][\x80-\xBF]|\xEF[\x81-\x83][\x80-\xBF]~', '', $first_name); echo $first_name; from PHP of course (PHP open / close tags are <?php and ?>).Spruik
It is not matching in preg_replace. I just have emoji symbols(😀 😁 😂) how to decode it like x80....? So that i can preg_replace?Benedicto
According to wiki, emojis are encoded from U+1F600 to U+1F64F: en.wikipedia.org/wiki/Emoticons_(Unicode_block). Where did you get your numbers from?Gav
❤️ : this is double hearts at same location !! I have remove it : $text = str_replace("❤️","",$text);Reverberatory
R
12

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.

Reuben answered 5/1, 2016 at 16:26 Comment(2)
Upvoted! This is the 6th solution I found on SO, and the only one that worked for the particular string with which I was dealing.Eolian
This will also remove special characters like ñ or éLaquanda
S
2
function remove_emoji($string) {

    // Match Emoticons
    $regex_emoticons = '/[\x{1F600}-\x{1F64F}]/u';
    $clear_string = preg_replace($regex_emoticons, '', $string);

    // Match Miscellaneous Symbols and Pictographs
    $regex_symbols = '/[\x{1F300}-\x{1F5FF}]/u';
    $clear_string = preg_replace($regex_symbols, '', $clear_string);

    // Match Transport And Map Symbols
    $regex_transport = '/[\x{1F680}-\x{1F6FF}]/u';
    $clear_string = preg_replace($regex_transport, '', $clear_string);

    // Match Miscellaneous Symbols
    $regex_misc = '/[\x{2600}-\x{26FF}]/u';
    $clear_string = preg_replace($regex_misc, '', $clear_string);

    // Match Dingbats
    $regex_dingbats = '/[\x{2700}-\x{27BF}]/u';
    $clear_string = preg_replace($regex_dingbats, '', $clear_string);

    return $clear_string;
}

Found this answer at https://medium.com/coding-cheatsheet/remove-emoji-characters-in-php-236034946f51

Saturate answered 8/12, 2021 at 7:30 Comment(1)
This one is only one from this thread removing thumbs up emoji. (By the way this symbol was breaking saving string to utf8_general_ci field.)Centro
C
1

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);
}

Chalone answered 4/11, 2012 at 10:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.