简体   繁体   中英

Outputting file contents as UTF-8 leads to character encoding issues

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:

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;

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM