For anyone who needs it

this code is able to clean emoji elements from, for example, the instagram title in a feed.

function removeEmoji($text) {

    $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 flags (iOS)
    $regexTransport = '/[\x{1F1E0}-\x{1F1FF}]/u';
    $clean_text = preg_replace($regexTransport, '', $clean_text);
    

    return $clean_text;
}

I've tested it modifying aggregator.parser.inc to strip emoji code from instagram title, because I can't upgrade my database from utf8 to utf8mb4 and emoji code breaks my run cron.

Comments

mossill’s picture

very helpful thanks. saved me some time !!!

mossill’s picture

I'm thinking of using this with

Emoji for PHP

This way we can allow emoticons from various devices :)

Using your code to detect emoticons and only then, send it thru the php function:
emoji_docomo_to_unified($text);

I think I'll load this php file with
module_load_include

louisjimenez’s picture

thanks, this was really helpful! I went ahead and made this more concise:

function remove_emoji($text) {
  // Match Emoticons
  $regex_emoticons = '/[\x{1F600}-\x{1F64F}]/u';

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

  // Match Transport And Map Symbols
  $regex_transport = '/[\x{1F680}-\x{1F6FF}]/u';
  
  // Match flags (iOS)
  $regex_flags = '/[\x{1F1E0}-\x{1F1FF}]/u';

  return preg_replace(array($regex_emoticons, $regex_symbols, $regex_transport, $regex_flags), '', $text);
}
merrifield69’s picture

To make your code even more concise(if you don't mind missing the comments)

function remove_emoji($text) {
  return preg_replace('/[\x{1F600}-\x{1F64F}]|[\x{1F300}-\x{1F5FF}]|[\x{1F680}-\x{1F6FF}]|[\x{1F1E0}-\x{1F1FF}]/u', '', $text);
}

But this code only worked to an extent for me. To remove all emoji's I ended up adding on to this code to include the removal of more emoji's and account for unicode variation selectors. Haven't done very extensive testing with it so it is probably far from perfect and may not be very efficient yet, but from the few small tests that I have done it appears to remove every emoji that IOS 7 has to offer.

function remove_emoji($text){
  return preg_replace('/([0-9|#][\x{20E3}])|[\x{00ae}|\x{00a9}|\x{203C}|\x{2047}|\x{2048}|\x{2049}|\x{3030}|\x{303D}|\x{2139}|\x{2122}|\x{3297}|\x{3299}][\x{FE00}-\x{FEFF}]?|[\x{2190}-\x{21FF}][\x{FE00}-\x{FEFF}]?|[\x{2300}-\x{23FF}][\x{FE00}-\x{FEFF}]?|[\x{2460}-\x{24FF}][\x{FE00}-\x{FEFF}]?|[\x{25A0}-\x{25FF}][\x{FE00}-\x{FEFF}]?|[\x{2600}-\x{27BF}][\x{FE00}-\x{FEFF}]?|[\x{2900}-\x{297F}][\x{FE00}-\x{FEFF}]?|[\x{2B00}-\x{2BF0}][\x{FE00}-\x{FEFF}]?|[\x{1F000}-\x{1F6FF}][\x{FE00}-\x{FEFF}]?/u', '', $text);
}
capellic’s picture

Thanks. Your improved regular expression did the trick for me. The original wasn't getting this icon: http://www.iemoji.com/view/emoji/12/smileys/face-with-stuck-out-tongue-a...

levze’s picture

Hi!
I am new in Drupal. How to implement this code in drupal 7?

sbilde’s picture

Thanks for sharing lajical - Exactly what I needed!

ravimane23’s picture

Hello,

Thanks for posting the code here which helps me in my project.

When I used above code with TCPDF library and trying to generate PDF, code replaces the emoji characters with question mark "?".

I found another code given below, removes emoji characters and question mark "?" as well :

    $string = str_replace("?", "{%}", $string);
    $string = mb_convert_encoding($string, "ISO-8859-1", "UTF-8");
    $string = mb_convert_encoding($string, "UTF-8", "ISO-8859-1");
    $string = str_replace(array("?", "? ", " ?"), array(""), $string);
    $string = str_replace("{%}", "?", $string);
    return trim($string);
agudivad’s picture

Thanks alot Ravi, your suggestion really helped us :)