Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中,当我使用fwrite时,我不使用';无法获得正确的字符集_Php_File_Encoding - Fatal编程技术网

在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脚本本身

首先,将utf_编码插入到fwrite中,如下所示:

<?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
?>