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:
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 :
Citation:
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 :
Citation:
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.