Detect file encoding in PHP
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.
My code is basically:
$output = '';
foreach ($files as $filename) {
$output .= file_get_contents($filename) . "\n";
}
file_put_contents('combined.txt', $output);
Currently, at the start of a UTF8 file, it adds these characters in the output: 
Solution 1:
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.
Solution 2:
To make sure that the output is UTF-8, no matter what kind of input it was, I use this check:
if(!mb_check_encoding($output, 'UTF-8')
OR !($output === mb_convert_encoding(mb_convert_encoding($output, 'UTF-32', 'UTF-8' ), 'UTF-8', 'UTF-32'))) {
$output = mb_convert_encoding($content, 'UTF-8', 'pass');
}
// $output is now safely converted to UTF-8!
Solution 3:
mb_detect_encoding
function should be your last choice. That could return the wrong encoding. Linux command file -i /path/myfile.txt
is working great. In PHP you could use:
function _detectFileEncoding($filepath) {
// VALIDATE $filepath !!!
$output = array();
exec('file -i ' . $filepath, $output);
if (isset($output[0])){
$ex = explode('charset=', $output[0]);
return isset($ex[1]) ? $ex[1] : null;
}
return null;
}