dongmei8511 2016-12-07 14:25
浏览 41
已采纳

array_map产生奇怪的unicode字符

I'm newbie in PHP so sorry for any funny mistakes :(

I have a problem when try to get some unicode characters (Korean, actually) from database to an javascript array. I think after call array_map("utf8_encode", $row); the field I needed has weird character. This is the file to do that business:

<?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();

?>

I also set the main page and the script charset=utf8 like this:

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

and

<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>

The original code's here. I just add a test record with this sql command:

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

1条回答 默认 最新

  • dongpao5658 2016-12-07 14:33
    关注

    According to utf8_encode

    utf8_encode — Encodes an ISO-8859-1 string to UTF-8

    Since you're dealing with Korean characters, I suspect the strings are not ISO-8859-1 encoded.

    Depending on the database settings, there is no need to convert the strings at all.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题