PHP Curl UTF-8 字符集

2021-12-28 00:00:00 utf-8 character-encoding php

我有一个 php 脚本,它调用另一个网页并写入页面的所有 html,一切正常,但是存在字符集问题.我的 php 文件编码是 utf-8 并且所有其他 php 文件都可以正常工作(这意味着服务器没有问题).该代码中缺少什么,所有西班牙语字母看起来都很奇怪.附注.当我将这些奇怪的字符原始版本写入php时,它们看起来都很准确.

I have an php script which calls another web page and writes all the html of the page and everything goes ok however there is a charset problem. My php file encoding is utf-8 and all other php files work ok (that means there is no problem with server). What is the missing thing in that code and all spanish letters look weird. PS. When I wrote these weird characters original versions into php, they all look accurate.

header("Content-Type: text/html; charset=utf-8");
function file_get_contents_curl($url)
{
    $ch=curl_init();
    curl_setopt($ch,CURLOPT_HEADER,0);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
    $data=curl_exec($ch);
    curl_close($ch);
    return $data;
}
$html=file_get_contents_curl($_GET["u"]);
$doc=new DOMDocument();
@$doc->loadHTML($html);

推荐答案

简单:当您使用 curl 时,它会将字符串编码为 utf-8 您只需要解码它们..

Simple: When you use curl it encodes the string to utf-8 you just need to decode them..

Description

string utf8_decode ( string $data )

此函数将假定为 UTF-8 编码的数据解码为 ISO-8859-1.

This function decodes data , assumed to be UTF-8 encoded, to ISO-8859-1.

相关文章