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

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.