1

When I call some facebook info from the opengraph I get some names that look like this.

\u0d94\u0db1\u0dca\u0dbd\u0dd2 \u0dc3\u0dd2\u0db1\u0dca\u0d9c\u0dca\u0dbd\u0dd2\u0dc2\u0dca

Is there a way to convert them in to readable unicode characters using PHP or Javascript? PHP is more preferred. Thanks

rksh
  • 3,920
  • 10
  • 49
  • 68
  • Did you search to see whether this question had already been answered? See, e.g. [this question](http://stackoverflow.com/questions/1365583/how-to-get-the-character-from-unicode-value-in-php) – cjh Sep 23 '12 at 15:18
  • @cjh it's in javascript, I'd rather prefer a way using PHP – rksh Sep 23 '12 at 15:21

2 Answers2

5

Use json_decode():

$str = '\u0d94\u0db1\u0dca\u0dbd\u0dd2';
echo json_decode('"'.$str.'"'); // ඔන්ලි

Codepad Example

Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
0

The following code allows you to decode the characters as well as re-encode them if necessary

Code :

if (!function_exists('codepoint_encode')) {

    function codepoint_encode($str) {
        return substr(json_encode($str), 1, -1);
    }

}

if (!function_exists('codepoint_decode')) {

    function codepoint_decode($str) {
        return json_decode(sprintf('"%s"', $str));
    }

}

How to use :

header('Content-Type: text/html; charset=utf-8'); 

var_dump(codepoint_encode('ඔන්ලි'));
var_dump(codepoint_encode('සින්ග්ලිෂ්'));

var_dump(codepoint_decode('\u0d94\u0db1\u0dca\u0dbd\u0dd2'));
var_dump(codepoint_decode('\u0dc3\u0dd2\u0db1\u0dca\u0d9c\u0dca\u0dbd\u0dd2\u0dc2\u0dca'));

Output :

string(30) "\u0d94\u0db1\u0dca\u0dbd\u0dd2"
string(60) "\u0dc3\u0dd2\u0db1\u0dca\u0d9c\u0dca\u0dbd\u0dd2\u0dc2\u0dca"
string(15) "ඔන්ලි"
string(30) "සින්ග්ලිෂ්"
John Slegers
  • 45,213
  • 22
  • 199
  • 169