据我了解,默认情况下 loadHTML 会加载拉丁语 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;
}
现在你可以看到我从网页中抓取数据,问题是这个词没有转换成 UTF-8。例如,“Az utolsó dal”需要蜜蜂“Az utolsó dal”。有人可以指导我解决问题或解决方案吗?