0

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

1 Answer 1

3

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.

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

3 Comments

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...
The parameter is a bit flag, to pass two OR them: JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT.
Oups I just found: json_encode($eleves, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); Thanks for your help!

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.