public function test()
{
$url = 'https://www.baidu.com/?tn=02003390_8_hao_pg';
$lines_array = file($url);
$lines_string = implode('', $lines_array);
file_put_contents("uploads/curlHtml/test.txt", $lines_string);
$encodings = $this->get_encoding($lines_string);
if ($encodings == 'GBK') {
$html = preg_replace('/charset=gb2312/', 'charset=UTF8', $lines_string);
$html = iconv('gbk', 'utf-8', $html);
} else {
$html = $lines_string;
}
echo $html;
}
function get_encoding($str = "")
{
$encodings = array(
'ASCII',
'UTF-8',
'GBK'
);
foreach ($encodings as $encoding) {
if ($str === mb_convert_encoding(mb_convert_encoding($str, "UTF-32", $encoding), $encoding, "UTF-32")) {
return $encoding;
}
}
return false;
}
|