IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage PHP Discussion :

probleme entre utf-8 et json_encode


Sujet :

Langage PHP

  1. #1
    Membre actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Décembre 2007
    Messages
    696
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Décembre 2007
    Messages : 696
    Points : 222
    Points
    222
    Par défaut probleme entre utf-8 et json_encode
    bonjour,

    j'essai de réaliser un autocomplete avec jquery et une base sous postgresql.
    l'aucomplete fonctionne avec toutes les chaines ne comportant pas de cacartères accentués.

    voici mon code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    header("Content-Type: text/plain; charset=UTF-8");
     
    $user='maxime';
    $pass='yuki2010';
    $dsn='pgsql:host=localhost;dbname=bd_my_valentine;';
     
    try
    {
    	$dbh = new PDO($dsn, $user, $pass);
    }
    catch (PDOException $e)
    {
    	print "Erreur ! : " . $e->getMessage();
    	die();
    }
     
    function get_cities($data)
    {
        global $dbh;
        $post_code = $data['post_code'];
        $term = preg_split("/ |-/", strtolower($data['city_name']));
        $max_rows = $data['max_rows'];
     
        // postcode
        $postcode_cond = ($post_code != null) ? "city_zip LIKE '".$post_code."%'" : null;
     
        // city name
        $i = 0;
        $city_cond = null;
        foreach($term as $keyword)
        {
            if($keyword != null)
            {
                $city_cond .= ($i > 0) ? ' AND ' : null;
                $city_cond .= " LOWER(city_name) LIKE '%".$keyword."%'";
                $i++;
            }
        }
     
        $cond = null;
        if($city_cond != null)
        {
            $cond .= ($postcode_cond != null) ? $postcode_cond.' AND ' : null;
            $cond .= $city_cond;
     
            $sql = "SELECT city_id AS id, city_name || ' (' || city_zip || ')' AS label, city_name || ' (' || city_zip || ')' AS value
                    FROM city
                    WHERE ".$cond."
                    ORDER BY city_zip, city_name
                    LIMIT ".$max_rows.";";
     
            $cities = $dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC);
            //echo $_GET['callback']."({'json_cities' : ".json_encode($cities)."});";
     
            echo print_r($cities);
        }
    }
    voici le retour :
    Array
    (
    [0] => Array
    (
    [id] => 59e31d40-5302-4fcb-adc0-9f015cd0cfc0
    [label] => Saint-Aubin-Epinay (76160)
    [value] => Saint-Aubin-Epinay (76160)
    )

    [1] => Array
    (
    [id] => 0de8bfc8-0011-42fe-bed3-42096bba8ecf
    [label] => Saint-Jacques-sur-Darn�tal (76160)
    [value] => Saint-Jacques-sur-Darn�tal (76160)
    )

    [2] => Array
    (
    [id] => 7c409702-fdb7-4b03-88d1-162b1ee3fe68
    [label] => Saint-L�ger-du-Bourg-Denis (76160)
    [value] => Saint-L�ger-du-Bourg-Denis (76160)
    )

    [3] => Array
    (
    [id] => 61263b87-885c-4fbc-86d6-f2b9d9f1904c
    [label] => Saint-Martin-du-Vivier (76160)
    [value] => Saint-Martin-du-Vivier (76160)
    )

    )
    1
    et si j'utilise un header avec un charset ISO-8859-1, j'obtiens ceci :
    Array
    (
    [0] => Array
    (
    [id] => 59e31d40-5302-4fcb-adc0-9f015cd0cfc0
    [label] => Saint-Aubin-Epinay (76160)
    [value] => Saint-Aubin-Epinay (76160)
    )

    [1] => Array
    (
    [id] => 0de8bfc8-0011-42fe-bed3-42096bba8ecf
    [label] => Saint-Jacques-sur-Darnétal (76160)
    [value] => Saint-Jacques-sur-Darnétal (76160)
    )

    [2] => Array
    (
    [id] => 7c409702-fdb7-4b03-88d1-162b1ee3fe68
    [label] => Saint-Léger-du-Bourg-Denis (76160)
    [value] => Saint-Léger-du-Bourg-Denis (76160)
    )

    [3] => Array
    (
    [id] => 61263b87-885c-4fbc-86d6-f2b9d9f1904c
    [label] => Saint-Martin-du-Vivier (76160)
    [value] => Saint-Martin-du-Vivier (76160)
    )

    )
    1
    les caractères passent bien, mais après un json_encode, seules les villes sans accents sont toujours acceptées.
    pourriez-vous m'aider svp.

  2. #2
    Expert éminent sénior

    Profil pro
    Inscrit en
    Septembre 2010
    Messages
    7 920
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2010
    Messages : 7 920
    Points : 10 726
    Points
    10 726
    Par défaut
    ta connexion a pgsql est bien utf-8 ? les tables aussi ?

  3. #3
    Membre actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Décembre 2007
    Messages
    696
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Décembre 2007
    Messages : 696
    Points : 222
    Points
    222
    Par défaut
    non, pour n'avoir aucun problème avec les ; et les &, j'ai créé une base de données en latin1.

  4. #4
    Expert éminent sénior

    Profil pro
    Inscrit en
    Septembre 2010
    Messages
    7 920
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2010
    Messages : 7 920
    Points : 10 726
    Points
    10 726
    Par défaut
    Citation Envoyé par thor76160 Voir le message
    non, pour n'avoir aucun problème avec les ; et les &, j'ai créé une base de données en latin1.
    c'est l'inverse les problèmes sont souvent à cause du latin1,

    si t'as base est en latin1 tu dois tout faire en iso-8859-1
    sinon utilise utf8_(d)encode

    ou mieux pas la base en utf-8

  5. #5
    Membre actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Décembre 2007
    Messages
    696
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Décembre 2007
    Messages : 696
    Points : 222
    Points
    222
    Par défaut
    je me demande si symfony gère ce genre de problème ?
    à l'avenir, tout le coté php/sql sera géré par symfony, aussitot que la version 2 sera stable.
    alors suis-je entrain de me tracasser la tête pour rien et devrais-je attendre que Symfony2 soit sorti ? O_o

  6. #6
    Membre actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Décembre 2007
    Messages
    696
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Décembre 2007
    Messages : 696
    Points : 222
    Points
    222
    Par défaut
    ok merci, je vais tester.

    edit :
    ça ne change rien. le header de mon fichier php est en ISO-8859-1, ainsi que mon fichier apellant (index.php si tu préfères).

  7. #7
    Expert éminent sénior

    Profil pro
    Inscrit en
    Septembre 2010
    Messages
    7 920
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2010
    Messages : 7 920
    Points : 10 726
    Points
    10 726
    Par défaut
    il faut simplement TOUT en utf-8

  8. #8
    Membre actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Décembre 2007
    Messages
    696
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Décembre 2007
    Messages : 696
    Points : 222
    Points
    222
    Par défaut
    mais donc =, j'aurais des problème quand je saisirais des ; et des & non ?
    sinon, je peux changer l'encodage sans risque ? ça ne va va endommager ma base avec des problème d'encodage j'espère ?

  9. #9
    Membre actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Décembre 2007
    Messages
    696
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Décembre 2007
    Messages : 696
    Points : 222
    Points
    222
    Par défaut
    voilà j'ai reconverti ma base en utf8, ça marche !
    merci bien

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Socket] Probleme entre un Client C et un serveur JAVA
    Par bpy1401 dans le forum API standards et tierces
    Réponses: 3
    Dernier message: 28/02/2006, 08h40
  2. probleme entre IE et Golive
    Par zipsy dans le forum Balisage (X)HTML et validation W3C
    Réponses: 2
    Dernier message: 01/02/2006, 16h51
  3. [PostgreSQL] [PostgreSQL] Probleme entre PHP & PostgreSQL
    Par flo78 dans le forum PHP & Base de données
    Réponses: 7
    Dernier message: 13/01/2006, 10h51
  4. Réponses: 8
    Dernier message: 30/12/2005, 13h47
  5. [MFC] enorme probleme entre statics et includes
    Par giova_fr dans le forum MFC
    Réponses: 4
    Dernier message: 09/12/2005, 14h15

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo