Reputation: 80629
I set my header as follows:
header( 'Content-Type: text/html; charset="utf-8"' );
and then output a local file on my server to the browser using the following code-segment:
$content = file_get_contents($sPath);
$content = mb_convert_encoding($content, 'UTF-8');
echo $content;
The files I have on the server are created by lua and thus, the output of the following is FALSE
(before conversion):
var_dump( mb_detect_encoding($content) );
The files contain some characters like ™
(™
) etc. and these appear as plain square boxes in browsers. I've read the following threads which were suggested as similar questions and none of the variations in my code helped:
.txt
s)There seem to be no problems when I simply use the following:
header( 'Content-Type: text/html; charset="iso-8859-1"' );
// setting path here
$content = file_get_contents($sPath);
echo $content;
Upvotes: 1
Views: 2583
Reputation: 522005
There seem to be no problems when I simply use the following:
header( 'Content-Type: text/html; charset="iso-8859-1"' ); // setting path here $content = file_get_contents($sPath); echo $content;
So this means the file content is actually encoded in ISO-8859-1. If you want to output this as UTF-8, then explicitly convert from ISO-8859-1 to UTF-8:
$content = mb_convert_encoding($content, 'UTF-8', 'ISO-8859-1');
You always need to know what you're converting from. Just telling PHP to "convert to UTF-8" and leaving it guessing what to convert from has an undefined outcome, and in your case it does not work.
Upvotes: 2
Reputation: 12306
Check the file encoding, is it utf-8 without BOM? For example, use the notepad++ for check file encoding.
Or mayby it's usefull:
$content = file_get_contents($sPath);
$content = htmlentities($content);
echo $content;
Or try in .htaccess:
AddDefaultCharset utf-8
AddCharset utf-8 *
<IfModule mod_charset.c>
CharsetSourceEnc utf-8
CharsetDefault utf-8
</IfModule>
Upvotes: 0