Created
June 29, 2020 06:33
-
-
Save MakStashkevich/647cb05105209f08ca67d5c2b084ea24 to your computer and use it in GitHub Desktop.
Php converter emojies to unicode
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
<?php | |
// Strips leading zeros | |
// And returns str in UPPERCASE letters with a U+ prefix | |
function format($str) { | |
$copy = false; | |
$len = strlen($str); | |
$res = ''; | |
for ($i = 0; $i < $len; ++$i) { | |
$ch = $str[$i]; | |
if (!$copy) { | |
if ($ch != '0') { | |
$copy = true; | |
} | |
// Prevent format("0") from returning "" | |
else if (($i + 1) == $len) { | |
$res = '0'; | |
} | |
} | |
if ($copy) { | |
$res .= $ch; | |
} | |
} | |
return 'U+'.strtoupper($res); | |
} | |
function convert_emoji($emoji) { | |
// ✊🏾 --> 0000270a0001f3fe | |
$emoji = mb_convert_encoding($emoji, 'UTF-32', 'UTF-8'); | |
$hex = bin2hex($emoji); | |
// Split the UTF-32 hex representation into chunks | |
$hex_len = strlen($hex) / 8; | |
$chunks = array(); | |
for ($i = 0; $i < $hex_len; ++$i) { | |
$tmp = substr($hex, $i * 8, 8); | |
// Format each chunk | |
$chunks[$i] = format($tmp); | |
} | |
// Convert chunks array back to a string | |
return implode($chunks, ' '); | |
} | |
echo convert_emoji("✊🏾"); //return U+270A U+1F3FE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment