Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve JSON_ERROR_UTF8 error in php json_decode?

I am trying this code

$json = file_get_contents("http://www.google.com/alerts/preview?q=test&t=7&f=1&l=0&e"); print_r(json_decode(utf8_encode($json), true));          //////////////  // Define the errors. $constants = get_defined_constants(true); $json_errors = array(); foreach ($constants["json"] as $name => $value) {     if (!strncmp($name, "JSON_ERROR_", 11)) {         $json_errors[$value] = $name;     } }  // Show the errors for different depths. foreach (range(4, 3, -1) as $depth) {     var_dump(json_decode($json, true, $depth));     echo 'Last error: ', $json_errors[json_last_error()], PHP_EOL, PHP_EOL; } 

I've tried a lot of functions, html_entities_decode, utf8_encode and decode, decoding the hex codes, but I always get the error "JSON_ERROR_UTF8".

How could I solve this?

like image 445
James Harzs Avatar asked Apr 17 '12 20:04

James Harzs


People also ask

How to fix json_ error_ utf8?

Note that since PHP 5.3. 3, there is a JSON_ERROR_UTF8 error returned when an invalid UTF-8 character is found in the string. This is a strong indication that a different charset than UTF-8 is used. If the incoming string is not under your control, you can use the utf8_encode function to convert it into utf8.

What is json_decode PHP?

The json_decode() function is used to decode or convert a JSON object to a PHP object.

What is Json_encode and json_decode in PHP?

JSON data structures are very similar to PHP arrays. PHP has built-in functions to encode and decode JSON data. These functions are json_encode() and json_decode() , respectively. Both functions only works with UTF-8 encoded string data.


1 Answers

There is a good function to sanitize your arrays.

I suggest you use a json_encode wrapper like this :

function safe_json_encode($value, $options = 0, $depth = 512, $utfErrorFlag = false) {     $encoded = json_encode($value, $options, $depth);     switch (json_last_error()) {         case JSON_ERROR_NONE:             return $encoded;         case JSON_ERROR_DEPTH:             return 'Maximum stack depth exceeded'; // or trigger_error() or throw new Exception()         case JSON_ERROR_STATE_MISMATCH:             return 'Underflow or the modes mismatch'; // or trigger_error() or throw new Exception()         case JSON_ERROR_CTRL_CHAR:             return 'Unexpected control character found';         case JSON_ERROR_SYNTAX:             return 'Syntax error, malformed JSON'; // or trigger_error() or throw new Exception()         case JSON_ERROR_UTF8:             $clean = utf8ize($value);             if ($utfErrorFlag) {                 return 'UTF8 encoding error'; // or trigger_error() or throw new Exception()             }             return safe_json_encode($clean, $options, $depth, true);         default:             return 'Unknown error'; // or trigger_error() or throw new Exception()      } }  function utf8ize($mixed) {     if (is_array($mixed)) {         foreach ($mixed as $key => $value) {             $mixed[$key] = utf8ize($value);         }     } else if (is_string ($mixed)) {         return utf8_encode($mixed);     }     return $mixed; } 

In my application utf8_encode() works better than iconv()

like image 132
Konstantin Avatar answered Oct 03 '22 08:10

Konstantin