0

I have used json_encode($response) to list the all the files in the directory. Since it has nearly 8000 records in the array of $response, when displayed in the page, it returns null.

I have research about this issue. So far, I could not find any solution.

This is my code:

$response['content'] = Files::$output;
echo json_encode($response);
Files::$output` /* this will return the list of files in the html format. */

When I try echo json_last_error_msg();, it returns:

Malformed UTF-8 characters, possibly incorrectly encoded

9
  • 1
    How are you displaying it on the page, can you post your code? Commented Jun 12, 2015 at 7:05
  • have you try to change the memory_limit parameter in php.ini ? Commented Jun 12, 2015 at 7:08
  • In my php.ini i have set memory limit 128M Commented Jun 12, 2015 at 7:11
  • How exactly have you concluded that the issue is the size of the input and not something else? Have you checked json_last_error_msg? Commented Jun 12, 2015 at 7:12
  • 1
    So your problem turns out to be a duplicate of stackoverflow.com/questions/14868096/… Commented Jun 12, 2015 at 8:28

1 Answer 1

0

JSON can only accept valid UTF characters when encoding them via json_encode.

Odds are, you have malformed input (as your error message says), which is more than likely some variant of a windows code page.

To ensure that your array is free of invalid JSON data, you can either convert it at a database layer (assuming you are using a database), or do the following (this assumes you have malformed latin1/ISO-8859-1 as the cause of your bad data):

foreach ($row as $key => &$value) {
    if ( ! mb_check_encoding($value, "UTF-8")) {
        // assume base is latin1; your mileage may vary
        $value = mb_convert_encoding($value, "UTF-8", "ISO-8859-1");
    }
}

You can't really force json_encode to parse your invalid data, so you're going to have to massage your dataset into valid UTF-8.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.