在PHP中,当我使用fwrite时,我不使用';无法获得正确的字符集
这是我的密码:在PHP中,当我使用fwrite时,我不使用';无法获得正确的字符集,php,file,encoding,Php,File,Encoding,这是我的密码: <?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
<?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
?>
当我打开文件时,我得到:
óéü
我试图使用fwrite保存大量数据,但在文件保存时字符编码不正确
提前感谢。您使用什么程序“打开”文件?该程序可能是问题所在。code>fwrite存储二进制字符串。它不进行任何字符集转换。 很可能您的PHP脚本位于错误的字符集,因此原始的
“óêü”
字符串。如果您自己无法调试,请向我们展示bin2hex($str)
和bin2hex(文件获取内容('demo.txt'))
有一些通用选项可解决此类问题:
- 保存前使用
utf8\u编码($str)
- 首先将UTF-8 BOM写入输出文件
fwrite($f,“\xEF\xBB\xBF”)
- 使用
iconv()正确转换
- 或者使用
recode L1..UTF8 script.php调整php脚本本身
<?php
$fName = "demo.txt";
$str = "óé";
fid = fopen($fName, 'wb') or die("can't open file"); // Open file
fwrite($fid, utf_encode($str)); // Write to file
fclose($fid); // Close file
?>
可能会尝试为长时间的延迟道歉。事实证明,观众才是问题所在。我有一些文本编辑器,根据我使用的编辑器,我会得到UTF-8字符集或不!我结束了使用尾波的一切,并没有任何其他问题。
<?php
$fName = "demo.txt";
$str = "óé";
fid = fopen($fName, 'wb') or die("can't open file"); // Open file
fwrite($fid, utf_encode($str)); // Write to file
fclose($fid); // Close file
?>