Created
August 12, 2016 07:50
-
-
Save melice/276e6836d7165dc5984dca7b0955c6ef to your computer and use it in GitHub Desktop.
php file_get_contents loadHTML 乱码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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