0

i've seen some posts about this topic.

I have a string that contains the letter é. I need to json_encode this string, but the function will return null. In other posts it is said that the reason behind this, is the fact that the string is not a utf-8 encoded string. So, it needs to be a utf-8 encoded string to work.

The problem is, the string comes directly from the database, and the table it originates from is encoded as utf-8. So how is the string i'm trying to parse to a json object not utf-8? Am i missing something?

David
  • 1,227
  • 12
  • 23

1 Answers1

0

Have you tried encoding it into UTF-8 and then saving it into the database using something like iconv(), or maybe before parsing it to a json ?

IE:

$utfchar = iconv('Windows-1252', 'UTF-8','é');

EDIT:

You Also need to set the browser's charset to utf8, this for example works:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
    $test = iconv('Windows-1252', 'UTF-8','é');
    echo $test;
?>

<script type="text/javascript">
    var jstest = '<?php echo $test; ?>';
    console.log(jstest);
</script>
Meridion
  • 53
  • 5
  • Well, it is a little more complex than that. The php is not in a HTML page. It is PHP only, and it only returns a string. There is no HTML involved. That content is then loaded through Ajax into a page that does have its charset set to utf-8. So the only thing i do is get the content from a utf8 encoded table and echo it's json encoded string. – David Jun 05 '14 at 08:25