1

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);
?>
Grant Miller
  • 27,532
  • 16
  • 147
  • 165
rouzbeh
  • 11
  • 1
  • 1
  • 8
  • Make sure that both, the file you are reading and the PHP script that reads it, are UTF-8 encoded (UTF-8 without BOM). – hherger Feb 28 '16 at 07:30
  • Sorry but, I did not catch. – rouzbeh Feb 28 '16 at 07:49
  • If the file you are reading is already utf-8 encoded you need to convert nothing at all. But the php script must be utf-8 encoded too. Otherwise it does not handle the file contents correctly. You can change the encoding of the script file by a couple of editors. One of them is Notepad++. – hherger Feb 28 '16 at 11:25
  • Plus: functions like _mb\_convert\_encoding_ are **converting** characters into utf-8. If the original file does not contain utf-8 characters the script can not create them. – hherger Feb 28 '16 at 11:29

2 Answers2

4

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));
}
?>
Samir Selia
  • 7,007
  • 2
  • 11
  • 30
  • hmmm, now try this : `$result = mb_convert_encoding($last_file, 'HTML-ENTITIES', "UTF-8");` –  Feb 28 '16 at 07:41
  • oh my god, Sorry my friend but This is not. for sample url test this link : http://www.varzesh3.com/news/1307397/ – rouzbeh Feb 28 '16 at 07:47
  • i want recive "http://www.varzesh3.com/news/1307397/" html codes. but when i use [ file_get_contents ] and echo this cant view then.This site does not display a code that allows.. – rouzbeh Feb 28 '16 at 08:25
  • please try `$content = @file_get_contents($fn);` then if not work , we can make decision –  Feb 28 '16 at 08:28
  • no no no Sorry, Please test yourself. echo file_get_contents(http://www.varzesh3.com/news/1307397/%22); – rouzbeh Feb 28 '16 at 08:32
  • you must act like this : `echo file_get_contents(http://www.varzesh3.com/news/1307397/%22); ` –  Feb 28 '16 at 08:34
0

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

header('Content-Type: text/html; charset =utf-8');
Michael
  • 1,089
  • 1
  • 11
  • 28