file_get_contents() converts UTF-8 to ISO-8859-1

前端 未结 4 643
灰色年华
灰色年华 2020-12-15 02:10

I am trying to get search results from yahoo.com.

But file_get_contents() converts UTF-8 charset (charset, that yahoo uses) content to ISO-8859-1.<

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-15 02:55

    For anyone investigating on this:

    The time I spent on encoding issues taught me that rarely php functions "magically" change the encoding of strings. (One of these rare examples is :

    exec( $command, $output, $returnVal )

    Please note also that the working header set is as follows:

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

    and not:

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

    As I had a similar issue as the one you describe, it was enough to set the headers properly.

    Hope this helps!

提交回复
热议问题