問(wèn)題描述
我有一個(gè) php 腳本,它調(diào)用另一個(gè)網(wǎng)頁(yè)并寫(xiě)入頁(yè)面的所有 html,一切正常,但是存在字符集問(wèn)題.我的 php 文件編碼是 utf-8 并且所有其他 php 文件都可以正常工作(這意味著服務(wù)器沒(méi)有問(wèn)題).該代碼中缺少什么,所有西班牙語(yǔ)字母看起來(lái)都很奇怪.附注.當(dāng)我將這些奇怪的字符原始版本寫(xiě)入php時(shí),它們看起來(lái)都很準(zhǔn)確.
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);
推薦答案
簡(jiǎn)單:當(dāng)您使用 curl 時(shí),它會(huì)將字符串編碼為 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 )
此函數(shù)將假定為 UTF-8
編碼的數(shù)據(jù)解碼為 ISO-8859-1
.
This function decodes data , assumed to be UTF-8
encoded, to ISO-8859-1
.
這篇關(guān)于PHP Curl UTF-8 字符集的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!