在PHP中,当我使用fwrite时,我没有得到正确的字符集 [英] In PHP, when I use fwrite, I don't get the correct character set

查看:158
本文介绍了在PHP中,当我使用fwrite时,我没有得到正确的字符集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

<?php
  header("Content-Type: text/html; charset=UTF-8");
  header("Content-Type: application/x-javascript; charset=UTF-8");

  $fName = "demo.txt";
  $str   = "óé";
  fid   = fopen($fName, 'wb') or die("can't open file"); // Open file

  fwrite($fid, $str); // Write to file
  fclose($fid);         // Close file
?>

在屏幕上,输出为:

óéü

当我打开文件我得到: / p>

When I open the file I get:

óéü

我正在尝试使用fwrite保存大量数据,但文件保存点的字符未正确编码。

I am trying to save large amounts of data using fwrite, but the characters are not encoding correctly at the point of file save.

谢谢提前。

推荐答案

fwrite 存储字符串二进制。它不会进行任何字符集转换。
您的PHP脚本更可能是错误的字符集,因此原来的óéü字符串。显示 bin2hex($ str) bin2hex(file_get_contents('demo.txt'))如果可以'你可以自己调试一下。

fwrite stores strings binary. It does not do any charset conversion. It's more likely that your PHP script is in a wrong charset, and thus the original "óéü" string. Show us the bin2hex($str) and bin2hex(file_get_contents('demo.txt')) if you can't debug it yourself.

有一些常见的选项来解决这些问题:

There are some generic options to solve such problems:


  • 在保存前使用 utf8_encode($ str)

  • 首先将UTF-8 BOM写入输出文件 fwrite($ f,\xEF\xBB\xBF)

  • 正确转换 iconv()

  • 或使用重新编码的代码L1..UTF8 script.php

  • Using utf8_encode($str) before saving.
  • Writing the UTF-8 BOM into the output file first fwrite($f, "\xEF\xBB\xBF")
  • correct conversion with iconv()
  • or adapting the php script itself with recode L1..UTF8 script.php

这篇关于在PHP中,当我使用fwrite时,我没有得到正确的字符集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆