Skip to content

Instantly share code, notes, and snippets.

@idiazroncero
Created June 12, 2020 06:55
Show Gist options
  • Save idiazroncero/afd4a30c554091ae3dd7d30f770733b0 to your computer and use it in GitHub Desktop.
Save idiazroncero/afd4a30c554091ae3dd7d30f770733b0 to your computer and use it in GitHub Desktop.
Remove Emoji characters from a string (PHP)
// All credit goes to Ravikant Mane:
// Original comment: https://www.drupal.org/forum/support/post-installation/2013-07-16/removing-emoji-code#comment-11307453
// Preserve the question marks by converting them to '{%}' placeholders.
$string = str_replace("?", "{%}", $string);
// Encode to and from an encoding type that doesn't support emojis.
// This will convert all emojis to the same character, in this case "question mark".
$string = mb_convert_encoding($string, "ISO-8859-1", "UTF-8");
$string = mb_convert_encoding($string, "UTF-8", "ISO-8859-1");
// Replace all occurrences of the question mark character.
$string = str_replace(array("?", "? ", " ?"), array(""), $string);
// Recover our original, legit question marks.
$string = str_replace("{%}", "?", $string);
// Done!
return trim($string);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment