About the problem of PHP receiving emoji conversion storage?

encountered a problem when typing emoji on the phone (unable to store it in the database).
the reason for the utf8mb4 of the database when it is found after searching for the problem (because emoji is 4 bytes, while utf-8 "s database is 3 bytes).
then get a js function that converts emoji emoticons into utf-8 characters to save

function utf16toEntities(str) { //utf16emoji 
         var patt=/[\ud800-\udbff][\udc00-\udfff]/g;
         str = str.replace(patt, function(char){
               var H, L, code;
               if (char.length===2) {   //
                     H = char.charCodeAt(0); // 
                     L = char.charCodeAt(1); // 
                     code = (H - 0xD800) * 0x400 + 0x10000 + L - 0xDC00; // 
                     return "&-sharp" + code + ";";
               } else {
                     return char;
               }
         });
         return str;
      }

the problem is that I want to convert this function into a PHP function. Please

Mar.11,2021

can't you use utf8mb4 for the relevant fields in the database? why do you have to change it


when you save it directly $value = json_encode ($value) can't you save it in json format? Just json_decode () parse


convert 4-byte UTF-8 into UCS-4 via iconv. For specific methods, please refer to this article:
PHP- deals with emoji emoticons

Menu