php获取网页标题和内容函数(不包含html标签)_PHP教程

WBOY
풀어 주다: 2016-07-13 10:39:57
원래의
867명이 탐색했습니다.

复制代码 代码如下:

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 way 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']) 200 or !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); //从header 里取charset  
        if( trim($matches[1]) ) {  
            $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


使用例子

复制代码 代码如下:

$a = getPageContent(www.ttphp.com);  
print_r($a);

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/728081.htmlTechArticle复制代码 代码如下: function getPageContent($url) { //$url='http://www.ttphp.com; $pageinfo = array(); $pageinfo[content_type] = ''; $pageinfo[charset] = ''; $pageinfo[title...
관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!