PHP to solve the DOM garbled method sample code _php Tips

Source: Internet
Author: User

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.

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: info-contact@alibabacloud.com 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.