PHP to solve the DOM garbled method

Source: Internet
Author: User
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.


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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.