Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将文件内容输出为UTF-8会导致字符编码问题_Php_Utf 8_Character Encoding_File Get Contents - Fatal编程技术网

Php 将文件内容输出为UTF-8会导致字符编码问题

Php 将文件内容输出为UTF-8会导致字符编码问题,php,utf-8,character-encoding,file-get-contents,Php,Utf 8,Character Encoding,File Get Contents,我的标题设置如下: header( 'Content-Type: text/html; charset="utf-8"' ); 然后使用以下代码段将“我的服务器”上的本地文件输出到浏览器: $content = file_get_contents($sPath); $content = mb_convert_encoding($content, 'UTF-8'); echo $content; 我在服务器上拥有的文件是由lua创建的,因此,以下输出为FALSE(转换前): 这些文件包含一些字

我的标题设置如下:

header( 'Content-Type: text/html; charset="utf-8"' );
然后使用以下代码段将“我的服务器”上的本地文件输出到浏览器:

$content = file_get_contents($sPath);
$content = mb_convert_encoding($content, 'UTF-8');
echo $content;
我在服务器上拥有的文件是由lua创建的,因此,以下输出为
FALSE
(转换前):

这些文件包含一些字符,如
™
)等,这些在浏览器中显示为普通的方形框。我已经阅读了以下作为类似问题建议的线程,但代码中的任何变体都没有帮助:

  • (在我的例子中没有gzip,文件是普通的
    .txt
    s)
  • (尝试了前两个最高级别的解决方案,但均无效。第三个不适用于我的案例)
  • (没有可作为上下文提供的流)
当我简单地使用以下内容时,似乎没有问题:

header( 'Content-Type: text/html; charset="iso-8859-1"' );
// setting path here
$content = file_get_contents($sPath);
echo $content;
header( 'Content-Type: text/html; charset="iso-8859-1"' );
// setting path here
$content = file_get_contents($sPath);
echo $content;

检查文件编码,是否为无BOM的utf-8?例如,使用notepad++检查文件编码

或者可能是有用的:

$content = file_get_contents($sPath);
$content = htmlentities($content);
echo $content;
或在.htaccess中尝试:

AddDefaultCharset utf-8
AddCharset utf-8 *
<IfModule mod_charset.c>
    CharsetSourceEnc utf-8
    CharsetDefault utf-8
</IfModule>
AddDefaultCharset utf-8
添加字符集utf-8*
CharsetSourceEnc utf-8
字符集默认utf-8
当我简单地使用以下内容时,似乎没有问题:

header( 'Content-Type: text/html; charset="iso-8859-1"' );
// setting path here
$content = file_get_contents($sPath);
echo $content;
header( 'Content-Type: text/html; charset="iso-8859-1"' );
// setting path here
$content = file_get_contents($sPath);
echo $content;
这意味着文件内容实际上是用ISO-8859-1编码的。如果要将其输出为UTF-8,请显式地从ISO-8859-1转换为UTF-8:

$content = mb_convert_encoding($content, 'UTF-8', 'ISO-8859-1');

你总是需要知道你是从什么转变过来的。只是告诉PHP“转换为UTF-8”并让它猜测转换的结果是未定义的,在您的情况下,它不起作用。

字符串似乎是有效的iso-8859-1。你试过简单的utf8编码而不是mb\U转换编码吗?@db mobile是的,我试过了。结果仍然是方形框。您的浏览器显示了什么作为检测到的编码?@db mobile Opera Dragonfly检测到字符集为UTF-8,然后您是否也尝试在不进行任何转换的情况下输出该文件?命令
file 02_01_2014.txt
返回:非ISO扩展ASCII文本,具有很长的转换行,您可以使用,它是从GNU/Linux上的libc包或重新编码安装的。有更多的选项和更好的错误处理。也许对你有用?我无法转换磁盘上的文件。对不起(这不会产生任何结果。根本没有输出。使用
htmlspecialchars
htmlspecialchars
是否有任何错误?如果我站对了,您希望将浏览器中的普通方形框替换为普通符号?但是
mb\u detect\u编码($content)
会导致错误。
mb\u convert\u编码($content,'UTF-8','ISO-8859-1')
也不起作用。千万不要指望任何东西
mb_detect_encoding
说,检测编码基本上不可能有任何程度的准确性。你首先需要知道的是文件的实际编码方式。如果不知道这一点,你将一事无成。
file--mime myfile.txt
:text/plain;charset=unknown-8bit。正如我所说的,文件是用lua创建的。即使lua也必须用一些已知的、指定的编码来编写文件。弄清楚这是什么。也许你可以让它直接编写UTF-8。