0

I have a CSV file that looks like this:

http://ideone.com/YWuuWx

I read the file and convert it to array, which works completely fine, but then I jsonize the array - but json_encode doesnt put the real values - it puts null - here is the dump of the array and jsonized array:

http://jave.jecool.net/stackoverflowdemos/csv_to_json_to_arraydump.php

I convert like this: $php_array= json_encode($json_array,JSON_PRETTY_PRINT);

anyone knows what might cause the problem?

EDIT: I think ther is like 90% chance that its caused by the latin1 characters - anyone knows the best workaround?

6
  • not really, those characters will be encoded too, in your case it will become D\u0159evo Commented Sep 3, 2013 at 10:27
  • Well this somehow does not happen - see this bugs.php.net/bug.php?id=49588 someone already had this problem, however htmlentities doesnt help my problem it evens return empty string x) Commented Sep 3, 2013 at 10:33
  • 1
    Did you actually check with json_last_error() to see if it's an encoding error? Commented Sep 3, 2013 at 10:36
  • try encoding your text as UTF-8 rather than Latin1. Commented Sep 3, 2013 at 10:47
  • @TML last error says "Error: 5" , - Spudley - Latin1 is a character set not an encoding, its encoded in UTF-8 ... the CSV file && Ive even turned on mb_internal_encoding("UTF-8"); just to be sure... Commented Sep 3, 2013 at 10:51

1 Answer 1

2

Assuming that it is in fact an encoding error, and that your data is actually encoded in some ISO-8859 variant (I'm guessing latin2 rather than latin1 based on your use of LATIN SMALL LETTER R WITH CARON), and that it is CONSISTENTLY so, you can use iconv() to re-encode it as UTF-8 before doing json_encode():

$foo = iconv('ISO-8859-2', 'utf8', $foo);
Sign up to request clarification or add additional context in comments.

10 Comments

You have a typo - encoding is utf-8 :) ! But with utf-8 it is working ! :) (Sorry for Latin1, it is Latin2 that was a multi-typo too :D ) thanks ! :) (please correct and I will accept :) )
I don't understand your follow-up question (nor what it has to do with accepting the answer to the question here). Also, the "utf-8" vs. "utf8" is entirely based on your own configuration of iconv - what works on your system would have given an invalid encoding error on mine :)
well tools.ietf.org/html/rfc3629 standard specifies it with hyphen :) - in this issue it is better to stay with standards and default settings :) - And the follow up question - reverse the conversion - is it iconv('utf8', 'ISO-8859-2', $foo)?, because iconv('ISO-8859-2', 'utf8', $foo) is good for json => javascript display but wrong for php echo :)
Well, iconv isn't part of RFC 3629 (and note that RFC 3629 specifies it as "UTF-8" not "utf-8"), and the names being used in the iconv() call are actually how iconv decides what filename it needs to open for the conversion process; whatever those file(s) are named on your server is the name you should use, otherwise iconv will tell you it's an invalid encoding.
No, they're not even related. default_charset dictates what PHP sends as the encoding in the HTTP headers.
|

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.