How fix UTF-8 Characters in PHP file_get_contents()

牧云@^-^@ 提交于 2019-12-11 10:40:25

问题


When I am using file_get_contents($url), and when I echo this, it returns an exotic character.

But it can be seen only in some websites and works correctly in other websites:

My code is:

<?php
header ( "Content-Type: text/html;charset=utf-8" );
$url ="http://www.varzesh3.com/news/1307290";
echo $go_to = file_get_contents($url);
?>

回答1:


According to PHP manual:

you can use this code, if you have problem with file_get_contents!

<?php
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));
}
?>



回答2:


and set header, it need to be first witch php send to client browser

header('Content-Type: text/html; charset =utf-8');


来源:https://stackoverflow.com/questions/35679900/how-fix-utf-8-characters-in-php-file-get-contents

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!