I want to encode \xf0\x9f\x98\xad
to \ud83d\ude2d
and decode back from \ud83d\ude2d
to \xf0\x9f\x98\xad
in PHP. Please suggest some functions.
I want to use this for emoji.
Actually my iPhone app gets \ud83d\ude2d
from an API and I've stored this in my database. Now to display that emoticon in the browser I have to convert it into \xf0\x9f\x98\xad
.
I've tried following class I got from internet.
Class file (cs.php) :
class Emoji
{
/**
* Encode emoji in text
* @param string $text text to encode
*/
public static function Encode($text) {
return self::convertEmoji($text,"ENCODE");
}
/**
* Decode emoji in text
* @param string $text text to decode
*/
public static function Decode($text) {
return self::convertEmoji($text,"DECODE");
}
private static function convertEmoji($text,$op) {
if($op=="ENCODE"){
return preg_replace_callback('/([0-9|#][\x{20E3}])|[\x{00ae}|\x{00a9}|\x{203C}|\x{2047}|\x{2048}|\x{2049}|\x{3030}|\x{303D}|\x{2139}|\x{2122}|\x{3297}|\x{3299}][\x{FE00}-\x{FEFF}]?|[\x{2190}-\x{21FF}][\x{FE00}-\x{FEFF}]?|[\x{2300}-\x{23FF}][\x{FE00}-\x{FEFF}]?|[\x{2460}-\x{24FF}][\x{FE00}-\x{FEFF}]?|[\x{25A0}-\x{25FF}][\x{FE00}-\x{FEFF}]?|[\x{2600}-\x{27BF}][\x{FE00}-\x{FEFF}]?|[\x{2900}-\x{297F}][\x{FE00}-\x{FEFF}]?|[\x{2B00}-\x{2BF0}][\x{FE00}-\x{FEFF}]?|[\x{1F000}-\x{1F6FF}][\x{FE00}-\x{FEFF}]?/u',array('self',"encodeEmoji"),$text);
}else{
return preg_replace_callback('/(\\\u[0-9a-fA-F]{4})+/',array('self',"decodeEmoji"),$text);
}
}
private static function encodeEmoji($match) {
return str_replace(array('[',']','"'),'',json_encode($match));
}
private static function decodeEmoji($text) {
if(!$text) return '';
$text = $text[0];
$decode = json_decode($text,true);
if($decode) return $decode;
$text = '["' . $text . '"]';
$decode = json_decode($text);
if(count($decode) == 1){
return $decode[0];
}
return $text;
}
}
My front file :
include "cs.php";
echo $a = Emoji::Encode("\xf0\x9f\x98\xad")."<br>"; // gives me \ud83d\ude2d
echo $b = Emoji::Decode($a); // gives me ðŸ˜
When I tried to encode the characters I got perfect result which is \ud83d\ude2d
. But when I tried to decode \ud83d\ude2d
I got some strange characters like ðŸ˜
.