dongzhong2018 2013-05-14 09:21
浏览 29
已采纳

包含utf8字符的PHP utf8_encode [关闭]

Background: A part of my system uses utf8_encode to convert html code to utf8 format before passing it on to json_encode.

Problem: Everything was fine until I entered UTF8 characters(Chinese) into the system. I noticed that the mentioned UTF8 characters were encoded twice and came out garbled.

Sidenote: I have no experience with charset encoding and whatnot until now. Perhaps I don't need to use utf8_encode before json_encode since my database and connections are already set to utf8. Without the Chinese characters in the html code, mb_detect_encoding would return ASCII(not ISO-8859-1). But I couldn't get through json_encode without returning null... thus the use of utf_8 encode which worked until now.

Update: I finally solved the issue by typecasting the html code as string; via (string)$html; before assigning it to json_encode().

Thanks to all who posted here that led me to the final solution.

  • 写回答

1条回答 默认 最新

  • duangou6446 2013-05-14 09:39
    关注

    I had similar problem. Maybe this would help: try to use iconv();. I had some problems with Polish characters (ąężćźłóń etc.)  in encodings other that utf8 - when I used json_encode, there was no output. Everything went just fine after using iconv();. The catch is, you have to know source encoding before you proceed. If you don't know encoding, use mb_detect_encoding() as @Lukas suggested. Example:

    $content = iconv('ISO-8859-2','UTF-8', $content);
    echo json_encode(array('content' => $content));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 java代码写在记事本上后在cmd上运行时无报错但又没生成文件
  • ¥15 关于#python#的问题:在跑ldsc数据整理的时候一直抱这种错误,要么--out识别不了参数,要么--merge-alleles识别不了参数(操作系统-linux)
  • ¥15 PPOCRLabel
  • ¥15 混合键合键合机对准标识
  • ¥100 现在不懂的是如何将当前的相机中的照片,作为纹理贴图,映射到扫描出的模型上
  • ¥15 魔霸ROG7 pro,win11.息屏后会显示黑屏,如图,如何解决?(关键词-重新启动)
  • ¥15 有没有人知道这是哪里出了问题啊?要怎么改呀?
  • ¥200 C++表格文件处理-悬赏
  • ¥15 Windows Server2016本地登录失败
  • ¥15 复合卡卡号轨道写入芯片卡