Skip to content

Instantly share code, notes, and snippets.

@VeroLom
Created December 21, 2023 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VeroLom/c7582d3dc1c71932028ca2661d3ac8b5 to your computer and use it in GitHub Desktop.
Save VeroLom/c7582d3dc1c71932028ca2661d3ac8b5 to your computer and use it in GitHub Desktop.
Converts Unicode Emojii to HTML Entities
<?php
function to_unicode($text) {
$str = preg_replace_callback(
"%(?:\xF0[\x90-\xBF][\x80-\xBF]{2} | [\xF1-\xF3][\x80-\xBF]{3} | \xF4[\x80-\x8F][\x80-\xBF]{2})%xs",
function($emoji){
$emojiStr = mb_convert_encoding($emoji[0], 'UTF-32', 'UTF-8');
return preg_replace("/^[0]+/","&#x",bin2hex($emojiStr) . ';');
},
$text
);
return $str;
}
echo to_unicode( 'hello world 💙' );
// hello world &#x1f499;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment