Php obtains the webpage title and description based on the website.

Source: Internet
Author: User
Php obtains the webpage title and description based on the website.
Namespace Xuyaoxiang; class Snoopy {private $ pattern_array = array ('title' => '/(\ S *. *) <\/title>/I ', 'description' => '/
 /', 'Charset' =>'/charset = \"? ([\ W-] + )\"? /I ',); public $ url; public $ target_code = "UTF-8"; // target code: public $ user_agent = 'mozilla/5.0 (Windows NT 10.0; WOW64) appleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36 '; // simulate the browser header data function _ construct ($ url) {$ this-> url = $ url;} public function set_pattern ($ key, $ val) {if (isset ($ this-> pattern_array [$ key]) {return false;} $ this-> pattern_array [$ key] = $ val;} // only rules can be added. The original rule function get_peg cannot be modified. E_content ($ pattern_key) {if ($ pattern_key = '') {return false;} $ curl = curl_init (); // Set the URLcurl_setopt ($ curl, CURLOPT_URL, $ this-> url); // sets headercurl_setopt ($ curl, CURLOPT_HEADER, 0); // sets the cURL parameter, and requires that the result be saved to the string or output to the screen. Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ curl, CURLOPT_USERAGENT, $ this-> user_agent); // run cURL to request the webpage $ data = curl_exec ($ curl ); if ($ data = false) {return false;} preg_match ($ this-> pattern_array ['charset'], $ data, $ reg_charset ); // Get the webpage code $ page_charset = strtolower ($ reg_charset [1]); $ charset = $ this-> target_code | $ data = mb_convert_encoding ($ data, $ this-> target_code, $ page_charset); // transcode the preg_match ($ this-> pattern_array [$ pattern_key], $ data, $ content); // curl_close ($ curl) of the URL request; return trim ($ content [1]); }}$ snoopy = new snoopy (" http://www.qq.com "); $ Title = $ snoopy-> get_pege_content ('title'); print_r ($ title );

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.