PHP get page title and content function _php Tutorial

Source: Internet
Author: User
Sometimes we need to get the title and content of the page, is a collection function, here simple to share, easy-to-use friend code as follows: function Getpagecontent ($url) {//$url = ' http://www.ttphp.com; PageInfo = Array (); $pageinfo [Content_Type] = '; $pageinfo [CharSet] = '; $pageinfo [title] = '; $pageinfo [description] = '; $pageinfo [keywords] = '; $pageinfo [body] = '; $pageinfo [' httpcode '] = 200; $pageinfo [' all '] = '; $ch = Curl_init (); curl_setopt ($ch, Curlopt_useragent, "mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0) "); curl_setopt ($ch, curlopt_returntransfer,1); curl_setopt ($ch, curlopt_ssl_verifyhost, 0); curl_setopt ($ch, curlopt_ssl_verifypeer,0); curl_setopt ($ch, curlopt_timeout, 8); curl_setopt ($ch, Curlopt_filetime, 1); curl_setopt ($ch, curlopt_followlocation, 1); curl_setopt ($ch, Curlopt_header, 1); curl_setopt ($ch, Curlopt_url, $url); $curl _start = Microtime (true); $store = curl_exec ($ch); $curl _time = Microtime (True)-$curl _start; if (Curl_error ($ch)) {$pageinfo [' httpcode '] = 505;//gate-it error echo ' Curl error: '. Curl_error ($ch). " /n "; Return $pageinfo; }//print_r (Curl_getinfo ($ch)); $pageinfo [' httpcode '] = Curl_getinfo ($ch, Curlinfo_http_code); Echo Curl_getinfo ($ch, Curlinfo_content_type). " /n "; $pageinfo [Content_Type] = Curl_getinfo ($ch, Curlinfo_content_type); if (Intval ($pageinfo [' Httpcode ']) <>/!preg_match (' @text/html@ ', Curl_getinfo ($ch, Curlinfo_content_type) ) {//print_r (Curl_getinfo ($ch));//exit; return $pageinfo;} preg_match ('/charset= ([^/s/n/r]+)/I ', Curl_getinfo ($ch, Curlinfo_content_type), $matches); Take CharSet if (Trim ($matches [1]) from the header {$pageinfo [charset] = Trim ($matches [1]);}//echo $pageinfo [CharSet]; Exit Curl_close ($ch); Echo $store; Remove JavaScript $store = Preg_replace ("/ $store = Preg_replace ("/ /smui ",", $store); Remove

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.