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 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分