Skip to content

Instantly share code, notes, and snippets.

@melice
Created August 12, 2016 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save melice/276e6836d7165dc5984dca7b0955c6ef to your computer and use it in GitHub Desktop.
Save melice/276e6836d7165dc5984dca7b0955c6ef to your computer and use it in GitHub Desktop.
php file_get_contents loadHTML 乱码
function loadNprepare($url,$encod='') {
$content = file_get_contents($url);
if (!empty($content)) {
if (empty($encod))
$encod = mb_detect_encoding($content);
$headpos = mb_strpos($content,'<head>');
if (FALSE=== $headpos)
$headpos= mb_strpos($content,'<HEAD>');
if (FALSE!== $headpos) {
$headpos+=6;
$content = mb_substr($content,0,$headpos) . '<meta http-equiv="Content-Type" content="text/html; charset='.$enc$
}
$content=mb_convert_encoding($content, 'HTML-ENTITIES', $encod);
}
$dom = new DomDocument;
$res = $dom->loadHTML($content);
if (!$res) return FALSE;
return $dom;
}
//$html = file_get_contents("compress.zlib://".$url);
//$doc = new DOMDocument();
//$doc->loadHTML($html);
$doc = loadNprepare($url,'utf-8');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment