How do i show emojis in php?

I have setup a twiml app in twillio which forward/post all sms (which came to my twillio number) to a domain . On that domain i can get message body.issue is when some one send emojis in SMS . for this i did json_encode($sms->body); which show me emojis like this. "\u263a\ud83d\ude00\ud83d\ude01\ud83d\ude02\ud83d\ude03" (these are some emojis which came through SMS)

i did json_decode() as well for above text but it not shows me correct emojis icons. it shows like this (☺😀ðŸ˜ðŸ˜‚😃)

Which encoding or decoding i should use please help me.

asked May 24, 2016 at 10:30

3

You just need to follow these steps 1. make sure you have set header("Content-Type: text/html; charset=utf-8"); 2. use this method

function decodeEmoticons($src) {
    $replaced = preg_replace("/\\\\u([0-9A-F]{1,4})/i", "&#x$1;", $src);
    $result = mb_convert_encoding($replaced, "UTF-16", "HTML-ENTITIES");
    $result = mb_convert_encoding($result, 'utf-8', 'utf-16');
    return $result;
}
$src = "\u263a\ud83d\ude00\ud83d\ude01\ud83d\ude02\ud83d\ude03";
echo decodeEmoticons($src);

it will shows you the emojis icons

answered May 24, 2016 at 13:30

See first of all check in your database Collation and set utf8_general_ci after that check your table Collation and set utf8_unicode_ci OR utf8_general_ci

answered May 24, 2016 at 13:19

How do i show emojis in php?

by Vincy. Last modified on July 3rd, 2022.

Online comments systems and chat applications will not be complete without an option to insert emojis. Emojis are emotive images to share emotions while sending messages. The Facebook-like social media websites popularized the usage of emoji by providing the messaging interface with emoji support.

Generally, people share emotions by creating emojis with symbols for example :-) ;-). By having the graphical emotive icons, it is fun to convey a variety of emotions through messages easily.

In this code, we are going to add an option to insert emojis while adding comments. We have already seen,  how to create a comment system using PHP. The same comments system example is going to be used in this example by integrating an emoji picker.

I have integrated a third-party library known as emoji-picker. This is a jQuery based library used to insert emoji picker control in the comment system interface. Download emoji-picker library and include it to the application.

How do i show emojis in php?

Comments System Interface with Emoji Picker

The HTML code shows the form to insert comments with emoji picker. There will be inputs for entering the user’s name and message while adding comments. Previously, we have added like/unlike and more features for a PHP comments system.

The emoji picker is initialized with the reference of the message box element. The comment box element has to be set with the HTML5 data attributes data-emojiable=true and data-emoji-input =unicode. After initialization, the emoji picker control will be displayed at the top right corner of the comment message box.








How to Insert Emoji using PHP in Comments








	

How to Insert Emoji using PHP in Comments

Comment Added Successfully!

jQuery Script to Enable Emoji-Picker

This is the code to add an emoji picker in the comment box. In this script, the EmojiPicker().discover() function is invoked, by setting the emoji selector that contains the data-emojiable attribute.

The emoji icons source path and more other default options will also set to the emoji picker instance at the time of initialization.

$(function () {
    // Initializes and creates emoji set from sprite sheet
    window.emojiPicker = new EmojiPicker({
        emojiable_selector: '[data-emojiable=true]',
        assetsPath: 'vendor/emoji-picker/lib/img/',
        popupButtonClasses: 'icon-smile'
    });

    window.emojiPicker.discover();
});

jQuery AJAX Code to List and Add Comments with Emojis

In this script, the comment-list and the comment-add request is raised from a jQuery AJAX function to the PHP. On submitting the user’s comment or replies with emoji selection, the AJAX call is executed to update the database.

After getting a response based on the action requested, the AJAX success block will handle the UI update dynamically. Comments and replies are shown in a hierarchical order as parent-child UI elements.


PHP Code to Add List Comments

The PHP and jQuery AJAX part have no magic and work as usual PHP AJAX programming that we learned already.

After adding the user comments along with the emojis to the database, the PHP code will print the comments which will be read as a response in the AJAX script. This response data will be updated in the comments UI.

On a successful database insert, a success message will be displayed in the comments form. In a previous tutorial, we have seen how to enrich comment system by displaying response text with jQuery fade in / fade away effect.

comment-list.php

query($sql);
if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        array_push($record_set, $row);
    }
}
echo json_encode($record_set);
?>

comment-add.php

prepare($query);

$param_type = "dsss";
$param_value_array = array(
    $commentId,
    $comment,
    $commentSenderName,
    $date
);
$param_value_reference[] = & $param_type;
for ($i = 0; $i < count($param_value_array); $i ++) {
    $param_value_reference[] = & $param_value_array[$i];
}
call_user_func_array(array(
    $sql_stmt,
    'bind_param'
), $param_value_reference);

$sql_stmt->execute();
?>

Database design

CREATE TABLE IF NOT EXISTS `tbl_comment` 
(
`comment_id` int(11) NOT NULL,
`parent_comment_id` int(11) DEFAULT NULL,
`comment` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
`comment_sender_name` varchar(40) CHARACTER SET utf8 NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
)ENGINE=InnoDB AUTO_INCREMENT=90 DEFAULT CHARSET=utf8mb4;

PHP Comments System Output with Emoji Selector

This screenshot shows the output of the PHP comments system with emoji picker. It also shows the list of comments with emojis inserted with the text messages.

Download

↑ Back to Top

How do I use emojis in PHP?

The emoji picker is initialized with the reference of the message box element. The comment box element has to be set with the HTML5 data attributes data-emojiable=true and data-emoji-input =unicode. After initialization, the emoji picker control will be displayed at the top right corner of the comment message box.

How do I insert an emoji in HTML?

This character encoding information is specified by using tag in the head section. After declaration of charset, emojis can be added in HTML by using

and tags.

tag is used to insert the emoji in a new line whereas tag inserts the emoji in the same line.

How do I add emojis to my database?

mysqli_set_charset($db, "utf8mb4"); This will allow you to input emojis directly into the database table that has been set to Collation: utfmb4_bin. Make sure to set your column to utfmb4 as well.