0

I have text in a php variable $sentence. I want to display the actual emoji codes found in $sentence on my web page. Not the emoji graphical icons but the actual emoji codes themselves which are embedded in the sentence.

I have been trying all day to do this by reverse engineering code that is usually used to "remove emojis". No success.

Looking to display the actual codes to emojis embedded in text. Example ...

[twitter.com/ProfitTradeRoom/status/880439582063566848][1]

As you can see the fire emoji is in this tweet. The code for fire is 1F525. I want to see 1F525 on my screen when i echo out the php variable. Basically "1F525 HOT 1F525 stocks today" on my screen instead of the graphical emojis.

String taken straight from my MySQL database (copied and pasted) ...

    HOTstocks today
  • Possible duplicate of [PHP : writing a simple removeEmoji function](https://stackoverflow.com/questions/12807176/php-writing-a-simple-removeemoji-function) – Obsidian Age Jul 04 '17 at 02:11
  • So how will PHP know to convert those fire images to `1F525` ? Trick question -- it can't; at least, not without a library or array. What do you have in mind? How about this PHP emoji library: http://code.iamcal.com/php/emoji/ – mickmackusa Jul 04 '17 at 03:02
  • Associated question with lots of advice: https://stackoverflow.com/questions/25695605/best-way-to-render-twitter-emojis-from-twitter-in-html – mickmackusa Jul 04 '17 at 03:12
  • @mickmackusa thanks!! – Grant Friedline Jul 04 '17 at 03:18
  • I've just emailed Cal the author of that github project and asked him to come here and have a look at your question... we'll see if that gets a response. – mickmackusa Jul 04 '17 at 03:28
  • Wow ok! I will continue to self-solve. Maybe I have to prep the data differently before I save it to the database. That may be where the problem is. I'm just not sure. – Grant Friedline Jul 04 '17 at 03:33

1 Answers1

0

I found the answer! The issue was my mysql connection. I was pulling from mysql into php. None of my regular expressions were matching any emoji unicodes until I did this ...

mysql_set_charset('utf8mb4');

I put mysql_set_charset immediately after I defined the connection string. Now when I pull text with emojis it will match a preg_match / preg_replace. For example detecting the fire emoji in a sentence ...

$theFireEmojiCode = '/[\x{1F525}]/u';
preg_match($theFireEmojiCode, $sentence, $matches); 

Or I can do a preg_replace like this ...

$theFireEmojiCode = '/[\x{1F525}]/u';
preg_replace($theFireEmojiCode, "1F525", $sentence);

Hope this helps.