Example code of php Solution to DOM garbled code and dom sample code

Source: Internet
Author: User

Example code of php Solution to DOM garbled code and dom sample code

Preface

DOM is a new xml and html processing class in php. It can operate the DOM tree as conveniently as javascript. More information about XML processing is provided on the Internet, today, this article introduces php's solution to DOM Garbled text. Let's not talk about it. Let's look at the solution below.

The solution 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, scheme, 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/661'); 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 (200 = $ code) {// solves garbled characters 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 the 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');

Summary

The above is all about this article. I hope this article will help you in your study or work. If you have any questions, please leave a message.

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.