1

I need to strip the emoji from a string, we can store the info, but it breaks our xml export because of the special characters used in emoji. I'd like to use PHP to strip the emoji out before it ever makes it to the database . Here is a sample of one such emoji: 😊

I've tried multiple methods described in this thread: PHP : writing a simple removeEmoji function

However they do not work. Can someone please suggest a way to strip all of these?

What I've tried so far that does not work to strip the above example emoji:

function removeEmoji($string) {
    return preg_replace('/&#x(e[0-9a-f][0-9a-f][0-9a-f]|f[0-8][0-9a-f][0-9a-f])/i', '', $string);
}

I've also tried:

public static 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 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;
}

and also tried:

$text = preg_replace('/&#x(e[0-9a-f][0-9a-f][0-9a-f]|f[0-8][0-9a-f][0-9a-f])/i', '', $text);

None of these work, the script I'm using to call all of these is:

  $comments = remove_emoji($comments);
Community
  • 1
  • 1
KeithW
  • 31
  • 6
  • What's your input string look like? Also `remove_emoji` != `removeEmoji`. You are calling the correct function, right? – mpen Feb 11 '15 at 19:20
  • And why exactly does it break your XML? – Jon Feb 11 '15 at 19:27
  • `& followed by ; breaks xml. I need to strip the emoji like 😊 from the comments so it does not cause issues.` – KeithW Feb 11 '15 at 20:21
  • my call looks like: `$comments = remove_emoji($comments);` my function looks like the above: `function remove_emoji($text){ comments to long to fit }` – KeithW Feb 11 '15 at 20:22
  • I think you send an array instead of a string value. Did you figure it out? – Matricore Jun 30 '15 at 14:53
  • No, none of the solutions presented worked – KeithW Jul 27 '15 at 12:58
  • You say "& followed by ; breaks xml". This is not true, per se. Really, undefined character escape codes break XML, and `😊` is not a defined character escape sequence in most XML dialects. If you converted it to the actual *character* it may well work. – TRiG Jun 12 '17 at 16:41

0 Answers0