I'm creating a chat room using Javascript/jQuery, PHP and Websockets. I want to support Unicode emoticons. I have a DIV with emoticons and, when user click in one of them, the emoticon is inserted in an input box. Simple.
User press "enter" and the text is sent to PHP. But PHP can't understand the emoticon (shows "??"), so I did a function in Javascript to convert to \uXXXX format (4-bytes) before submit to PHP.
Example:
1) User submits "test😀test"
2) Javascript converts to "test\uD83D\uDE00test" (function below) - If I go to Chrome console and type "test\uD83D\uDE00test" it shows "test😀test" correctly, so, the converted string is correct.
3) Submit the string to PHP: "test\uD83D\uDE00test"
4) PHP sends to Websocket that will only append the string into a DIV, something like $('#chat').append(d), where "d" (received from socket.io object) contains "test\uD83D\uDE00test".
But it shows the string "test\uD83D\uDE00test"... I want to show the emoticon.
Where is my error?
Function to convert emoticons to \u format: scan all chars and modify only the non-ascii ones.
function ChatUCode(t) {
S='';
for (a=0;a<t.length;a++) {
if (t.charCodeAt(a)>255) {
S+='\\u'+('0000'+t.charCodeAt(a).toString(16)).substr(-4,4).toUpperCase();
} else {
S+=t.charAt(a);
}
}
return S;
}
$(...).append('test\uD83D\uDE00test')and vanilla js[...].innerHTML += 'test\uD83D\uDE00test')seem to work appropriately. This seems to me to be a translation across the network. That is, whatever is coming from the websocket thinks the string needs the literal` andu` and such. You may want to consider something like json-encoding and decoding, or some other method of encoding/decoding to prevent this issue (among other random issues from network translations)