0

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 😭.

psmears
  • 26,070
  • 4
  • 40
  • 48
Mr. Engineer
  • 3,522
  • 4
  • 17
  • 34
  • 1
    what's wrong with a simple str_replace? have you actually tried to code anything for this yet? – Franz Gleichmann Feb 09 '16 at 06:57
  • Yes I've tried. See my updated question. – Mr. Engineer Feb 09 '16 at 07:01
  • What emojis are you trying to show? Font file? Images? Not really getting what that code is supposed to do (seems a bit overcomplicated) and what the expected output would be? And where iPhones API (what API) has anything to do with this? – M. Eriksson Feb 09 '16 at 07:28
  • Nothing is related with iPhone API. it's working fine. It gives me `\ud83d\ude2d` and i stored it in my database. now i want to display message coming from iPhone API in my website. For that i want to convert `\ud83d\ude2d` into `\xf0\x9f\x98\xad` so that my browser can display smiley. See the example : http://codepad.org/Hb2E7HlO – Mr. Engineer Feb 09 '16 at 07:35
  • That code just shows me wierd characters. Are the smileys in a custom font file? Is it image sprites? There aren't any "global" emojis that works in all fonts/browsers. – M. Eriksson Feb 09 '16 at 07:39
  • No there is no images. Just view this link : http://codepad.org/Hb2E7HlO. – Mr. Engineer Feb 09 '16 at 07:42
  • I did.. and like I said, that codepad-snippet only gives me weird characters. No emojicon: http://prntscr.com/a0w8dq - Again, there aren't any global emoji-set that works in all broswers/character sets/OS's. – M. Eriksson Feb 09 '16 at 07:58
  • Please see my screenshot : http://prntscr.com/a0w9xu – Mr. Engineer Feb 09 '16 at 08:03
  • I don't know how to say this in any other way for you to not ignore it: *there aren't any global emoji-set that works in all broswers/character sets/OS's*. It depends on your browser/character set(font)/OS. – M. Eriksson Feb 09 '16 at 08:12
  • I got your point from your first comment.Do you have other way to display iPhone unicode characters in web? – Mr. Engineer Feb 09 '16 at 08:14
  • What exactly do you want to express with that notation, `\ud83d\ude2d` and `\xf0\x9f\x98\xad`? Neither of those strings taken literally will display any emoji. To display an emoji, you need to produce the binary string `0xf09f98ad` (). Is that what you're referring to? – In a nutshell, you want to convert the Unicode escape sequence notation `\ud83d\ude2d` into an actual binary string? – deceze Feb 09 '16 at 08:27
  • @deceze , Hey why you've marked as duplicate. that question don't even close to mine. They have different requirements. Please reopen this question. I still didn't get my answer. – Mr. Engineer Feb 09 '16 at 09:02
  • Those answers there *seem* extremely related. At least the decoding from Unicode escape notation to strings seems to be answered there. If that's not all you need, please clarify your question a bit. Contrast how the solutions there are *not* sufficient to solve your issue and why and it'll become clearer what exactly you *do* need. – deceze Feb 09 '16 at 09:07
  • I just want to display emoji stored in my database as `\ud83d\ude2d` on my website.`\ud83d\ude2d` is coming from iphone. iOS developer has encoded an emoji in UTF8 and send it to me. But how can i display that emoji in my browser as i only have `\ud83d\ude2d` in my database. – Mr. Engineer Feb 09 '16 at 09:10
  • Have you tried any of the solutions from the duplicate?! They work! – deceze Feb 09 '16 at 09:19
  • Yes I've tried solution as mentioned in accepted answer and it's not working. – Mr. Engineer Feb 09 '16 at 09:21
  • Well, the next one is! https://3v4l.org/XH9h6 – deceze Feb 09 '16 at 09:25
  • Then why its not working in my localhost.I've wrote same in localhost `json_decode('"\ud83d\ude2d"');` but it give me some strange character as mentioned in my question. – Mr. Engineer Feb 09 '16 at 09:30
  • Because you're not interpreting the result as UTF-8?! [Handling Unicode Front To Back In A Web App](http://kunststube.net/frontback/) – deceze Feb 09 '16 at 09:48
  • Oh yes, that's the case. Now it's working fine. – Mr. Engineer Feb 09 '16 at 09:55

0 Answers0