Reputation: 93
I am sending a string from iphone app to server. That string may contain Emoji - I am encoding and sending that. From server side they are showing it in an html page,How can they convert the string into EMOJI charecters
my string is like this
testing \ud83d\ude04 my \ud83d\udc0e Emoji \ud83c\udf85 hureee \u260e hai \ud83d\udeb2
Upvotes: 0
Views: 2049
Reputation: 539965
That looks like the JSON encoding of Unicode characters (as a UTF-16 surrogate pair). json_decode()
should properly decode that:
<?php
$json = '{"key":"testing \ud83d\ude04 my \ud83d\udc0e Emoji \ud83c\udf85 hureee \u260e hai \ud83d\udeb2"}';
$obj = json_decode($json);
echo $obj->{'key'};
?>
Output:
testing 😄 my 🐎 Emoji 🎅 hureee ☎ hai 🚲
Upvotes: 2
Reputation: 2030
You could create an array with the list of these particular strings along with another array containing their replacements (e.g. images), then use the PHP command preg_replace in this way:
<?php
$originalString = 'testing \ud83d\ude04 my \ud83d\udc0e Emoji \ud83c\udf85 hureee \u260e hai \ud83d\udeb2';
$patterns = array();
$patterns[0] = '/\\\\ud83d\\\\ude04/';
$patterns[1] = '/\\\\ud83d\\\\udc0e/';
$patterns[2] = '/\\\\ud83c\\\\udf85/';
$patterns[3] = '/\\\\ud83c\\\\udf85/';
$patterns[4] = '/\\\\u260e/';
$patterns[5] = '/\\\\ud83d\\\\udeb2/';
$replacements = array();
$replacements[5] = '<img src="image6URL" alt="alternative text" />';
$replacements[4] = '<img src="image5URL" alt="alternative text" />';
$replacements[3] = '<img src="image4URL" alt="alternative text" />';
$replacements[2] = '<img src="image3URL" alt="alternative text" />';
$replacements[1] = '<img src="image2URL" alt="alternative text" />';
$replacements[0] = '<img src="image1URL" alt="alternative text" />';
$newString = preg_replace($patterns, $replacements, $originalString);
echo $newString;
?>
$newString will be the resulting string that you can use as an output in your HTML page.
Notice that the backward slashes in the emoji must be escaped using 3 additional backward slashes in the regular expression.
Upvotes: 0