Created
June 12, 2020 06:55
-
-
Save idiazroncero/afd4a30c554091ae3dd7d30f770733b0 to your computer and use it in GitHub Desktop.
Remove Emoji characters from a string (PHP)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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