How do I properly get back an emoji unicode from JS to PHP?

381
July 13, 2017, at 04:46 AM

My php code is properly generating the following string in order to display an emoji

$text = <div>test 1<span>&#x1f607</span></div>

My php code is passing this string to my js code, which is receiving it and properly displaying the emoji in my web editor.

Within my js code, I can modify the text such that the string to be returned to php now looks like this (notice the unicode has not changed by design):

$text = <div>test 2<span>&#x1f607</span></div>

To pass the string back to php I am doing the following:

var send_form = $('#send_msg_form').serializeArray();
send_form.push({ name: "msg_text", value: msg_text });

My php code is properly receiving the data and I am able to obtain the msg_text from the incoming parms.

The problem is that I am no longer seeing the unicode within the string var in php. It is a wonky symbol that is apparently due to some unicode translation that is occurring when JS pushes or when php receives the data.

If I pull the string from php and paste it into an editor, I see the following and after the p there are 3 upside down question marks:

<div>test 1<span>p</span></div>

I obviously am missing something with regard to passing the unicode from js to php. I am not sure what to do next. I am currently running php version 5.6. Any help would be greatly appreciated.

Answer 1

It seems that your JS code passes the encoded UTF-8 smile to php code.

&#x1f607 - it is code of emoji.

So I assume (because of lack of information) that you get encoded string on PHP-side.

So if you want to display emoji related to code you can decode it with function html_entity_decode:

html_entity_decode('&#x1f607;');

Note that encoded entity must end with ";" symbol (I think the same must be true for JS editor).

READ ALSO
Sliders not showing up: setREVStartSize is not defined

Sliders not showing up: setREVStartSize is not defined

After I upgrade my Revolution slider plugin (54

1748
PHP - Modify array while sorting

PHP - Modify array while sorting

I need to sort an array of strings alphabetically and set each key to be the same as the value (because isset() is so much faster than in_array())I can easily do this by making two passes over the array, but I was curious if it's possible to do it in only one pass

272
Why is &amp;nbsp; sent from textarea counted as two letters?

Why is &nbsp; sent from textarea counted as two letters?

If I click the SUBMIT button in the above code, I will get the following output:

232
integrating jquery ui Autocomplete with a select box loaded from a server

integrating jquery ui Autocomplete with a select box loaded from a server

I have a page that loads a <select> box from a database

242