I am trying to loop through a set of javascript files, detect their encoding and then save them both in latin1 and in utf8 format. So far I have really gotten stuck at trying to get PHP to convert a UTF-8 file to Latin1. I have tried several different methods of encoding the data after searching around but nothing seems to work.
Here is what I am doing:
//Read file
$fileIn = fopen($dirName . $fileNameIn, 'r');
$dataIn = fread($fileIn, filesize($dirName . $fileNameIn));
fclose($fileIn);
$encoding = mb_detect_encoding($dataIn);
echo "Encoding in: " . $encoding . "\r\n";
$dataOut = utf8_decode($dataIn);
echo "Encoding after: " . mb_detect_encoding($dataOut) . "\r\n";
Both of these printouts print UTF-8 as the detected encoding. Any ideas?
Could this have anything to do with the fact that the system default on my ubuntu box is UTF-8?
You might want to take a look at the iconv function (http://www.php.net/manual/en/book.iconv.php):
Therefore, try writing
and see if that fixes the problem. Also, try using strict mode in mb_detect_encoding: