在PHP中检测文件编码 [英] Detect file encoding in PHP
问题描述
我有一个脚本,它将多个文件合并成一个文件,当其中一个文件具有UTF8编码时,它会断开。我认为我应该使用 utf8_decode()
函数读取文件时,但我不知道如何告诉哪个需要解码。
I have a script which combines a number of files into one, and it breaks when one of the files has UTF8 encoding. I figure that I should be using the utf8_decode()
function when reading the files, but I don't know how to tell which need decoding.
我的代码基本上是:
$output = '';
foreach ($files as $filename) {
$output .= file_get_contents($filename) . "\n";
}
file_put_contents('combined.txt', $output);
目前,在UTF8文件的开头,它在输出中添加了以下字符:
Currently, at the start of a UTF8 file, it adds these characters in the output: 
推荐答案
尝试使用 mb_detect_encoding
函数。这个函数将检查你的字符串,并试图猜它的编码是什么。然后,您可以根据需要进行转换。不过,如果 brulak建议,您最好将到 UTF-8,而不是 ,以保留您要传输的数据。
Try using the mb_detect_encoding
function. This function will examine your string and attempt to "guess" what its encoding is. You can then convert it as desired. As brulak suggested, however, you're probably better off converting to UTF-8 rather than from, to preserve the data you're transmitting.
这篇关于在PHP中检测文件编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!