問題描述
我有一個 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
.
這篇關于PHP Curl UTF-8 字符集的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!