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 😎
?
json_en/decode
for serialization:"this & that \ud83d\ude0e"
β Sum