dongpu8935 2015-04-17 09:34
浏览 554
已采纳

由PHP写入文件的JSON中的奇怪字符(�)

I have a PHP code which writes a JSON file when it's called. The file contains a few latin characters from spanish and portuguese language. It is displayed correctly on the browser when browsing it directly to it's URL, but in the application and in the developer console under the response preview tab, these characters are replaced by the character �.

Picture to better explain the situation: bad characters (Click picture to open in full size)

The file is written in PHP with the following code:

file_put_contents('../data/cameras.json', $json, LOCK_EX);

I tried many ways to fix this encoding with no success (some of them even removed the characters, but that's not an acceptable solution).

Any suggestions of how to solve this problem?

  • 写回答

2条回答 默认 最新

  • dougehe2022 2015-04-17 09:57
    关注

    I have downloaded the json-file and when looking at it in notepad++ in ANSI mode, it looks fine. So i guess the file is in WINDOWS-1252 encoding.
    And i am guessing that you want to use UTF8 in the file you write with PHP.

    try to convert the encoding of your string before you write the file:

    $json = mb_convert_encoding($json, "UTF-8", "WINDOWS-1252");
    
    file_put_contents('../data/cameras.json', $json, LOCK_EX);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看