Vishal Purohit
Vishal Purohit

Reputation: 261

Encode accentuated characters in json using php

I've a query result that contains some accentuated characters Like :

CollectionTitle => Afleuréss

But when i write a json file with json_encode($Result_Array) and retrieve the result it shows :

CollectionTitle => NULL  

then i used array_map() :

$res[] = array_map('utf8_encode', $row);

But it results me :

CollectionTitle => Afleuréss instead of CollectionTitle => Afleuréss

Please suggest me better way to resolve this issue.

Thanks

Upvotes: 1

Views: 298

Answers (4)

prashant
prashant

Reputation: 966

Add to your HTML head:

<meta http-equiv="content-type" content="text/html;charset=utf-8" />

Note that you should have a proper HTML doctype because browsers default to non utf8. You can do a simple test, like I did, this works:

<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<?php
$title = "Jérôme";
echo $title."<br>";

But the place for the meta tag is in the head tag. The HTML document should look like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>An XHTML 1.0 Strict standard template</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
</head>

<body>

<?php
$title = "Jérôme";
echo $title."<br>";
?>

That is standard.

Upvotes: 1

eightyfive
eightyfive

Reputation: 4731

In your second example / step:

$res[] = array_map('utf8_encode', $row);

It looks like you're trying to encode in UTF-8 something that is not ISO-8859-1.

You should detect / know what's the encoding coming from your Database, and transcode it properly to UTF-8 with iconv for example.

As an alternative, you should know:

  • What is the encoding in the Database?
  • What is the encoding of the PHP files?
  • What is the encoding in the HTML page? <meta charset="utf-8">

And if that's possible, move all of the above to UTF-8...

Upvotes: 0

Esailija
Esailija

Reputation: 140236

json_encode only supports UTF-8, but the rest of your app is using Windows-1252. I don't suggest using utf8_encode as that converts from ISO-8859-1 to UTF-8. That only works 95% of the time for you because you are using Windows-1252, not ISO-8859-1*.

I don't know if it's possible for you but if you can, you should switch over to UTF-8 so you don't need this fragile conversion code anywhere.

*This is probably confusing. Browsers do not actually allow you to use ISO-8859-1 and instead treat it as Windows-1252. Same with MySQL, Latin1 means Windows-1252. Both are defaults. utf8_encode/decode of course use actual ISO-8859-1, so it's incompatible in the 0x80-0x9F range.

Upvotes: 1

Bart Friederichs
Bart Friederichs

Reputation: 33573

The second one is actually the correct one. The problem is your browser cannot detect the encoding and defaults to whatever the default is (probably ISO-8859-1). Switch your browser encoding and you'll see the right character appear.

Upvotes: 2

Related Questions