我有以下代码:
/* change character set to utf8 */
if (!mysqli_set_charset($con, "utf8"))
{
printf("Error loading character set utf8: %s'n", mysqli_error($con));
} else {
printf("Current character set: %s'n", mysqli_character_set_name($con));
}
// query the application data
$sql = "SELECT * FROM names";
$result = mysqli_query($con, $sql);
// an array to save the application data
$rows = array();
// iterate to query result and add every rows into array
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
$rows[] = $row;
}
// close the database connection
mysqli_close($con);
// echo the application data in json format
echo json_encode($rows);
我在服务器 http://blabla.gr/apps.php 上尝试过,我得到''u0391''u03bd''u03c4''u03ce''u03bd''u03b7''u03c2 代替希腊字符
我怎样才能让它们正确显示为正常的 utf8 希腊字符。
默认情况下,json_encode
函数使用转义码对 Unicode 字符进行编码。您可以使用 JSON_UNESCAPED_UNICODE
标志使它们不转义。
echo json_encode($rows, JSON_UNESCAPED_UNICODE);