php 如何在PHP中设置文本文件编码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19067492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set text file encoding in PHP?
提问by Programmer.zip
How can I set a text file ENCODING (for instance UTF-8) in PHP?
如何在 PHP 中设置文本文件编码(例如 UTF-8)?
Let me show you my problem. This is my code:
让我告诉你我的问题。这是我的代码:
<?php
file_put_contents('test.txt', $data); // data is some non-English text with UTF-8 charset
?>
Output: ?§ù
!
输出:?§ù
!
fwrite()
has the similar output.
fwrite()
有类似的输出。
But when I create the test.txt
by notepad and set the charset UTF-8the output is what I want.
I wanna set the charset in the PHP file.
但是当我创建test.txt
by 记事本并设置字符集UTF-8 时,输出就是我想要的。我想在 PHP 文件中设置字符集。
Now this is my question: How to set text file encoding by PHP?
现在这是我的问题:如何通过 PHP 设置文本文件编码?
回答by Joni
PHP does not apply an encoding when storing text in a file: it stores data exactly as it is laid out in the string.
PHP 在文件中存储文本时不应用编码:它完全按照字符串中的布局存储数据。
You mention that you have problems opening the file in notepad.exe
. That text editor is not very good at guessing the encoding of the file you are opening; if the text is encoded in UTF-8 you must choose to open it as UTF-8. Use another text editor if possible. Notepad++is a popular replacement.
您提到在notepad.exe
. 那个文本编辑器不太擅长猜测你打开的文件的编码;如果文本以 UTF-8 编码,您必须选择将其打开为 UTF-8。如果可能,请使用其他文本编辑器。Notepad++是一种流行的替代品。
If you must use notepad.exe
, as a last resort, write a Byte Order Markto the file before you write anything else; this will make it recognize the file as UTF-8 while potentially making the file unusable for other purposes (see the Wikipedia article for details).
如果您必须使用notepad.exe
,作为最后的手段,请在写入任何其他内容之前将字节顺序标记写入文件;这将使其将文件识别为 UTF-8,同时可能使文件无法用于其他目的(有关详细信息,请参阅 Wikipedia 文章)。
file_put_contents("file.txt", "\xEF\xBB\xBF" . $data);
回答by The Alpha
You may try this using mb_convert_encoding
您可以尝试使用mb_convert_encoding
$data = mb_convert_encoding($data, 'UTF-8', 'auto');
file_put_contents('test.txt', $data);
Also check iconv.
还要检查iconv。
Update :(try this and find the right encoding for your text)
更新:(试试这个并为您的文本找到正确的编码)
foreach(mb_list_encodings() as $chr){
echo mb_convert_encoding($data, 'UTF-8', $chr)." : ".$chr."<br>";
}
Also, try this on GitHub.
另外,在GitHub 上试试这个。
回答by BAD_SEED
Try:
尝试:
file_put_contents('test.txt', utf8_encode($data));
回答by Asuquo12
You can create a function which converts a string array into a utf8 encoded string array and another to decode and write to a notepad file for you.
您可以创建一个函数,将字符串数组转换为 utf8 编码的字符串数组,并为您解码并写入记事本文件。
<?php
function utf8_string_encode(&$array){
$myencode = function(&$value,&$key){
if(is_string($value)){
$value = utf8_encode($value);
}
if(is_string($key)){
$key = utf8_encode($key);
}
if(is_array($value)){
utf8_string_encode($value);
}
};
array_walk($array,$func);
return $array;
}
?>