Recently encountered a problem in the work, in the use of the DOM, found the problem of garbled, and later through the search online data finally solved, and now will solve the method to share to everyone, interested friends can reference, have the needs of friends below to learn together.
Objective
Dom is PHP compared to the new XML and HTML processing class, can be like JavaScript as convenient operation of the DOM tree, the web is more about its handling of XML, the article today on the introduction of PHP to solve the DOM garbled method, the following words do not say, directly see the following solutions.
Here's how to fix it
/** * 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 (Preg_match = = $code) {//Solve garbled if (' #<meta[^>]*charset= '? gb2312 "[^>]*># ', $data)) {$data = i Conv ("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 ('
$html _content = mb_convert_encoding ($html _content, ' UTF-8 ', ' gb2312 ');
The above is the whole content of this article, I hope that everyone's study has helped.