0

I use

$link = json_decode(file_get_contents("http://graph.facebook.com/111866602162732"));

the result on that page shows:

 "name": "L\u00e9ry, Quebec",

I then want to convert that with the accents.. like this:

$location_name = $link->name;
echo 'NAME ORIGINAL: '.$location_name;
$location_name = preg_replace('/\\\\u([0-9a-fA-F]{4})/', '&#x\1;', $location_name); // convert to UTF8
echo '  NAME after: '.$location_name;

I get the following result:

  NAME ORIGINAL: Léry, Quebec     NAME after: Léry, Quebec

my preg_replace is correct, so it's the original name that is being transformed by the file_get_contents.

user3011784
  • 833
  • 1
  • 8
  • 30

2 Answers2

0

If file_get_contents don't give you back a well format UTF-8 text, then json_decode you would return NULL. Json MUST be in UTF-8 encoding.

This function only works with UTF-8 encoded strings. (json_decode)

So, I guess that you're reading the data with another encoding. Check it out.

Federkun
  • 36,084
  • 8
  • 78
  • 90
  • I don't understand what you mean by another encoding... my code is written above... – user3011784 Jan 04 '15 at 03:01
  • Seems that you're reading a utf-8 string with another charset. Be sure to read the data with the correct charset. See for http://kore-nordmann.de/blog/php_charset_encoding_FAQ.html#http-related – Federkun Jan 04 '15 at 03:07
  • just put `header( 'Content-type: text/html; charset=utf-8' );` at the top of your code, then read http://stackoverflow.com/questions/279170/utf-8-all-the-way-through – Federkun Jan 04 '15 at 03:10
0

Most likely, you're treating a valid UTF-8 output given to you by json_decode as ISO-8859-1 See here, for example: http://www.i18nqa.com/debug/bug-utf-8-latin1.html

Make sure that you're treating your debug output as UTF-8 - that should solve the problem.