繁体   English   中英

PHP的file_get_contents编码问题,显示损坏的文本

[英]php file_get_contents encoding issue of showing corrupted text

我确实在同一服务器中创建了html文件。 我需要将html文件内容读取为文本,并且为此使用了file_get_contents()函数。 当我这样做时,它返回的是损坏的文本,类似于我们在编码问题中看到的符号。 当我在文本编辑器中打开html文件时,使用常规字符就可以了。 我已链接 https://www.dropbox.com/s/y2iya9epd73c0r1/9663testme.docx.html?dl=0一个测试HTML文件,但file_get_contents无法正确读取/显示。

我确实使用了以下功能将读取的内容转换为UTF-8,但是尽管有一些进步,但有时它仍会给出损坏的文本。

 function file_get_contents_utf8($fn) { 
  $content = file_get_contents($fn); 
  return mb_convert_encoding($content, 'UTF-8', 
  mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true)); 
}  

如果有人可以指导我找到解决方案,可以使用file_get_contents或任何其他替代解决方案,我将不胜感激。

谢谢。

使用file_get_contents_utf8('9663testme.docx.html')进行调用;

function file_get_contents_utf8($fn) { 
  //$content = file_get_contents($fn); 
  //return mb_convert_encoding($content, 'UTF-8', 
  //mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true)); 
  echo file_get_contents($fn);
} 

这个很棒的小图书馆帮助我解决了这个问题。

ForceUTF8 https://github.com/neitanod/forceutf8

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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