Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a problem with my encoding...

Everything is UTF-8 configured, namely my database (postgres) and my php files.

when I execute this script:

$eleves = $serviceManager->getAll('Eleve');
echo "<pre>";
print_r($eleves);
echo "</pre>";

I get this:

 [0] => Model_Eleve Object
    (
        [idEleve:Model_Eleve:private] => 28206
        [numeroscolaire:Model_Eleve:private] => ABE290999JOËL
        [nom:Model_Eleve:private] => Abedinpour
        [prenom:Model_Eleve:private] => Joël
        [dateNaissance:Model_Eleve:private] => 1999-09-29
        [sexe:Model_Eleve:private] => masculin
        [statusCourant:Model_Eleve:private] =>
        [statusSuivant:Model_Eleve:private] =>
        [adresses:Model_Eleve:private] => Array
            (
                [0] => Model_Adresse Object
                    (
                        [idAdresse:Model_Adresse:private] => 6176
                        [rue:Model_Adresse:private] => La Delèze
                        [numero:Model_Adresse:private] => 37
                        [codePostal:Model_Adresse:private] => 1164
                        [localite:Model_Adresse:private] => Buchillon
                        [emplacement:Model_Adresse:private] =>
                    )

            )

Here everything's ok. But if I want JSON:

$eleves = $serviceManager->getAll('Eleve');
echo "<pre>";
echo json_encode($eleves, JSON_PRETTY_PRINT);
echo "</pre>";

I get this:

{
    "nom": "Abedinpour",
    "prenom": "Jo\u00ebl",
    "adresse": [
        {
            "rue": "La Del\u00e8ze",
            "numero": "37",
            "localite": "Buchillon"
        }
    ],
    "classe": [
        "7VSG\/1"
    ]
},

I have no idea why it works with array and doesn't work with json_encode... I've tried to transform everything in utf8 (with htmlentities) but it tells me that he can't convert 'ASCII'

When I try to show the encoding of my files with this command in the terminal:

file -I myfile.php

it returns this:

myfile.php: text/x-php; charset=us-ascii

but i can't convert it to utf8:

iconv -f us-ascii -t utf-8 myfile.php > myfile2.php
file -I myfile2.php
myfile2.php: text/x-php; charset=us-ascii

thanks in advance for your help

share|improve this question

1 Answer

up vote 0 down vote accepted

That is the perfectly valid JSON way to encode non-ASCII characters. Nothing is wrong here. Any client properly decoding this will retrieve the correct character. If you prefer actual UTF-8 characters and are running PHP 5.4+, use the JSON_UNESCAPED_UNICODE flag for json_encode.

share|improve this answer
yeah it works great! but is it possible to have both JSON_UNESCAPED_UNICODE and JSON_PRETTY_PRINT ? I've tried this: $eleves2 = json_encode($eleves, JSON_UNESCAPED_UNICODE); $eleves3 = json_encode($eleves2, JSON_PRETTY_PRINT); but it doesn't work... – theplayer777 20 hours ago
The parameter is a bit flag, to pass two OR them: JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT. – deceze 20 hours ago
Oups I just found: json_encode($eleves, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); Thanks for your help! – theplayer777 20 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.