array_map产生奇怪的unicode字符

时间:2016-12-07 14:25:46

标签: php unicode

我是PHP的新手,对任何有趣的错误感到抱歉:(

尝试从数据库到javascript数组获取一些unicode字符(实际上是韩语)时遇到问题。我想在电话array_map("utf8_encode", $row);之后,我需要的字段有奇怪的特征。这是开展业务的文件:

<?php
    include 'ChromePhp.php';
    ChromePhp::log('Hello console!');

    $mysql = new mysqli('localhost','root','vertrigo','demo', 3306);
    $mysql->set_charset("utf8");
    $result = $mysql->query("select * from countries");
    $rows = array();
    while($row = $result->fetch_array(MYSQL_ASSOC)) {
        $rows[] = array_map("utf8_encode", $row);
        ChromePhp::log($row); // fine, readable characters
    }
    ChromePhp::log($rows); // weird characters
    echo json_encode($rows);

    $result->close();
    $mysql->close();

?>

我还设置了主页面和脚本charset=utf8,如下所示:

<meta http-equiv="Content-Type" content="text/html; charset=utf8" />

<script src="lib/jquery/jquery-1.11.1.min.js" charset="utf8"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="lib/bootstrap/js/bootstrap.min.js" charset="utf8"></script>
    <script src="lib/magicsuggest/magicsuggest.js" charset="utf8"></script>
    <script src="js/script.js" charset="utf8"></script>

原始代码 here 。我只是用这个sql命令添加一个测试记录:

INSERT INTO `demo`.`countries` (`idCountry`, `countryCode`, `countryName`, `population`, `capital`, `continentName`) VALUES (NULL, 'KO', 'KOR', '134', '서울', 'Asia');

1 个答案:

答案 0 :(得分:1)

根据utf8_encode

  

utf8_encode - 将ISO-8859-1字符串编码为UTF-8

由于你正在处理韩文字符,我怀疑这些字符串不是 ISO-8859-1编码。

根据数据库设置,根本不需要转换字符串。