Objective
Dom is a new PHP XML and HTML processing class, can be as easy as JavaScript to operate the DOM tree, more on the web to deal with the situation of XML, today this article on the introduction of PHP to solve the DOM garbled method, the following words do not say more directly to see the following solution.
The workaround is as follows
/** * Request URL Page information * @param str $url * @return str mixed|boolean/function Curl_get ($url) {$curl = Curl_init ();
curl_setopt ($curl, Curlopt_url, $url);
curl_setopt ($curl, Curlopt_returntransfer, 1);
302 Jump Curl_setopt ($curl, curlopt_followlocation, 1); curl_setopt ($curl, Curlopt_useragent, ' mozilla/5.0 (Windows NT 6.1; WOW64;
rv:47.0) gecko/20100101 firefox/47.0 ');
curl_setopt ($curl, Curlopt_referer, $url);
$data = curl_exec ($curl); $code = Curl_getinfo ($curl, Curlinfo_http_code);
Output request Status Code curl_close ($curl); if (= = = $code) {//Resolve garbled if (Preg_match (' #<meta[^>]*charset= "? gb2312" [^>]*># ', $data)] {$data
= Iconv ("gb2312", "Utf-8//ignore", $data); $data = preg_replace (' #<meta[^>]*charset= ' gb2312 ' [^>]*> #is ', ' <meta http-equiv= ' Content-type ')
Content= "Text/html;charset=utf-8" > ", $data); } if (!preg_match (' #<meta charset= ' utf-8 ' [^>]*> #is ', $data)) {$data = Str_replace ('
/**
* Get DOMDocument object
* @param str $url
* @return boolean| DOM
*
/function GetDOM ($url) {
$html _content = Curl_get ($url);
if (Empty ($html _content)) {
//savelog ($url, ' request failed ');
return false;
}
$dom = new DOMDocument (' 1.0 ', ' utf-8 ');
Libxml_use_internal_errors (true);
$dom->loadhtml ($html _content);
return $dom;
}
$html _content = mb_convert_encoding ($html _content, ' UTF-8 ', ' gb2312 ');
Summarize
The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.