3

I am trying to show emoji using its unicode value(). But I am getting escaped string as \u00f0\u0178\u02dc\u20ac, which is decoded into 😀.

I am using Mysql server and PHP 5.4 in my project. In mysql, it's stored as 😀. Is there any way to unescape this and return Actual unicode from PHP server

I tried, iconv('ASCII//TRANSLIT', 'UTF-8', '😀');, mb_convert_encoding($var, "US-ASCII", "UTF-8") and utf8_encode(). not working.

Thanks

Abhijith A C
  • 530
  • 2
  • 7
  • 21
  • 1
    You need to use `utf8mb4` in MySQL. `utf8` only supports up to 3 bytes per character. – Vatev Jul 13 '15 at 12:44
  • [Here](http://apps.timwhitlock.info/unicode/inspect?s=%F0%9F%98%80) you can see it is indeed 4 bytes. – OIS Jul 13 '15 at 13:02
  • @Vatev : Mysql is bypassing storage problem by splitting it to 4 separate character. I am trying to restructure it back. Since my db is pretty huge, I can not directly change db encoding without proper validation. – Abhijith A C Jul 13 '15 at 13:12
  • @Vatev **[Mysql docs](https://dev.mysql.com/doc/refman/5.5/en/charset-unicode-utf8mb4.html) says the emoji does not have the 4th byte in the database** For a supplementary character, utf8 cannot store the character at all, while utf8mb4 requires four bytes to store it. Since utf8 cannot store the character at all, you do not have any supplementary characters in utf8 columns and you need not worry about converting characters or losing data when upgrading utf8 data from older versions of MySQL. – OIS Jul 13 '15 at 13:27

1 Answers1

0

Without knowing the structure of your database (make sure that you're using the utf8 as the character set for your table!), I think the problem may just be on the display side. Try starting your PHP script by sending a header to the browser that lets it know that you're going to be displaying UTF8 characters, rather than Western encoding (ISO-8859-1).

header('Content-type text/html; charset=UTF-8');
Seth Battis
  • 106
  • 8
  • Still the same input/output? I wonder if you've got bum data in the database -- have you looked at [this answer](http://stackoverflow.com/a/2446818/294171) and tried that for getting the emoji in (and out) of the database without corruption? (I tried storing the emoji in both a `TEXT` and a `BLOB` in the MySQL database and both seemed to work fine _without_ that.) – Seth Battis Jul 14 '15 at 13:27