Search code examples
phpemojiphp-5.3html-entitieshtml-encode

How to convert Emojis to their respective HTML code entities in PHP 5.3?


I need to convert the Emojis (e.g. 😀) in strings to their respective HTML code entities (e.g. 😀) on a PHP 5.3 site.

I need to do this so that user input gets properly stored in a legacy script MySQL Database to later display properly when shown back to the user. When attempting to save Emojis directly from user input, they are incorrectly saved as ? in its Database. This legacy script does not support utf8mb4 in MySQL (this solution failed) and all attempts at converting its Database, Tables, and Columns to utf8mb4 have not solved this problem, so the only solution I have left which I already confirmed works is converting user-inputted Emojis in strings to their respective HTML code entities to correctly store those entities as-is in the Database so that they display correctly as Emojis when retrieved since modern browsers automatically convert those Emoji entities to Emoji characters.

I have also tried this solution, but it does not work in PHP 5.3, only in 5.4 and above. (I cannot upgrade to 5.4 on this particular site because the legacy script it depends on only works in 5.3 and cannot be changed or upgraded under any circumstances.)

I have also tried this solution, which works in PHP 5.3, but you can't feed it a string, only the specific Emoji, so it does not solve my problem despite working in PHP 5.3.

I only need the Emojis in a string converted, nothing else. (However, if that is not possible, then I suppose I can live with other HTML entities being converted with it, like & to &, but I prefer that not be the case.)

So how can I convert Emojis in strings to their respective HTML code entities in PHP 5.3 such that a string like this & that 😎 gets converted to this & that 😎?


Solution

  • The code to detect the emoji bypasses stackoverflow's character limit, so here's a gist instead:

    https://gist.github.com/BarryMode/432a7a1f9621e824c8a3a23084a50f60#file-htmlemoji-php

    The entire function is essentially just

    preg_replace_callback(pattern, callback, string);
    

    The string is the input where you have emoji that you want to change into html entities. The pattern uses regex to find the emoji in the string and then each one is fed into the callback, which is where the conversion happens from emoji to html entity.

    In creating this function, htmlemoji(), I combined a few different pieces of code that others had worked on. Here's some credits:

    The callback uses this stackoverflow answer to build each entity.

    The pattern was directly ripped from this source on GitHub.