私が理解しているように、デフォルトで loadHTML は Latin 1 でロードされ、これを UTF-8 文字に変換したいと考えています。コードは次のとおりです。
// get data from website
function get_url_contents($url){
$crl = curl_init();
$timeout = 5;
curl_setopt ($crl, CURLOPT_ENCODING, 'UTF-8');
curl_setopt ($crl, CURLOPT_URL,$url);
curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
$ret = curl_exec($crl);
curl_close($crl);
return $ret;
}
// Now here is the domdoc
function get_all_meta_tags($html){
$html = get_url_contents($html);
$doc = new DOMDocument('1.0', 'UTF-8');
$doc->encoding = 'UTF-8';
$nodes = $doc->getElementsByTagName('title');
$title = $nodes->item(0)->nodeValue;
$arr['title']=$title;
$nodes = $doc->getElementsByTagName('h1');
$h1 = $nodes->item(0)->nodeValue;
$arr['h1']=$h1;
$metas = $doc->getElementsByTagName('meta');
for ($i = 0; $i < $metas->length; $i++)
{
$mt = $metas->item($i);
if($mt->getAttribute('name')=='description')
$dec=$mt->getAttribute('content');$arr['description']=$dec;
if($mt->getAttribute('name')=='keywords')
$key=$mt->getAttribute('content');$arr['keywords']=$key;
}
return $arr;
}
Webページからデータを取得していることがわかりますが、問題は単語がUTF-8に変換されないことです。たとえば、「Az utlsó dal」は「Az utlsó dal」である必要があります。誰かが私に問題または解決策を教えてもらえますか?