【发布时间】:2018-05-02 19:00:08
【问题描述】:
我需要以 utf-8 编码获取远程文件的内容。 utf-8 格式的文件。当我在屏幕上显示该文件时,它具有正确的编码:
http://www.parfumeriafox.sk/source_file.html
(注意ň 和č 字符,例如,这些都可以)。
当我运行这段代码时:
<?php
$url = 'http://parfumeriafox.sk/source_file.html';
$csv = file_get_contents_utf8($url);
header('Content-type: text/html; charset=utf-8');
print $csv;
function file_get_contents_utf8($fn) {
$content = file_get_contents($fn);
return mb_convert_encoding($content, 'utf-8');
}
(您可以使用http://www.parfumeriafox.sk/encoding.php 运行它),然后我得到问号而不是那些特殊字符。我对此进行了大量研究,我尝试了标准的file_read_contents 函数,我什至使用了一些流 bla bla php 上下文函数,我还尝试了 fopen 和 fread 函数来读取二进制级别的文件,似乎没有任何效果。我已经尝试过发送和不发送标题。这应该是完全简单的,我做错了什么?当我使用一些编码检测函数检查该字符串时,它返回UTF-8。
【问题讨论】:
标签: php utf-8 file-get-contents