【问题标题】:PHP Problems with writing string to text file将字符串写入文本文件的 PHP 问题
【发布时间】:2014-05-08 08:23:39
【问题描述】:

我已经创建了可以写文本的表单。

在 PHP 中,它将获取此文本字段的值并尝试将其写入文件:

$txtFile = $filePath.$counter."_ans.txt";
$f=fopen($txtFile, "wb");
fwrite($f, $word);
fclose($f);

当我用我的语言编写文本时,例如қазақ

我启动它。它创建了 .txt 文件,但当我打开它时,它显示:қазақ

问题:我必须做什么?我的语言是哈萨克语。

【问题讨论】:

  • 通过@deceze检查guide
  • 是的,我忘记在 html 中添加字符集
  • 但是你以.txt格式保存文件?

标签: php encoding utf-8 fwrite


【解决方案1】:

你需要用 UTF-8 编码你的文件,你有两个解决方案:

 header('Content-Type: text/html; charset=utf-8'); // Server side

或者

 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> // Client side

在文件的顶部。

【讨论】:

    【解决方案2】:

    您可以尝试在文件中添加 BOM(字节顺序标记)(并以 html 格式打开):

    $txtFile = $filePath.$counter."_ans.txt";
    $f=fopen($txtFile, "wb");
    $string = "\xEF\xBB\xBF";
    $string .= $word;
    fwrite($f, $string);
    fclose($f);
    

    但首先看看你的变量var_dump($word)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-06
      • 2013-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多