Search code examples
phputf-8file-get-contents

How fix UTF-8 Characters in PHP file_get_contents()


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:

see picture

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


Solution

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