欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-10-14 20:02:53
复制代码 代码如下:function getpagecontent($url) {        &n...
复制代码 代码如下:

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("/<mce:script.*><!--
(.*)<//script>/smui",'',$store);  
        //remove link   
        $store = preg_replace("/<link/s+[^>]+>/smui",'',$store);  
        //remove <!--  -->  
        $store = preg_replace("/<!--.*-->/smui",'',$store);  
        //remove <style  </<style>  
        $store = preg_replace("/<style.*>(.*)<//style>/smui",'',$store);  
        //remove 中文空格  
        $store = preg_replace("/ /",'',$store);  
        //remove 标点符号  
        //$store = preg_replace("/[/~`!@#$%^&*()_/-+={}|/[/]//;':"/</>/?/,/.//]/",'',$store);  

          
        //preg_match("/<head.*>(.*)<//head>/smui",$store, $matches);  
        //$head = $matches[1];  
        //echo $head. "/n";  

        //charset  
        if($pageinfo[charset] == '' ) { 
            preg_match('@<meta.+charset=([/w/-]+)[^>]*>@i',$store,$matches); 
            $pageinfo[charset] = trim($matches[1]); 
        } 
        //desctiption 
        preg_match('@<meta/s+name=/"*description/"*/s+content/s*=/s*([^/>]+)/*>@i',$store,$matches); 
        //print_r($matches); 
        $desc = trim($matches[1]); 
        $pageinfo[description] = str_replace("/"", '',$desc); 

 
        preg_match('@<meta/s+name=/"*keywords/"*/s+content/s*=/s*([^/>]+)/*>@i',$store,$matches); 
        //print_r($matches); 
        $keywords = trim($matches[1]); 
        $pageinfo[keywords] = str_replace("/"", '',$keywords); 

         
        preg_match("/<title>(.*)<//title>/smui",$store, $matches); 
        $pageinfo[title] = trim($matches[1]); 

        preg_match("/<body.*>(.*)<//body>/smui",$store, $matches); 
        $pageinfo[body] = addslashes( replacehtmlandjs($matches[1]) ) ; 
        $pageinfo['all'] = addslashes( replacehtmlandjs($store) ) ; 

        //echo "charset = " . $pageinfo[charset] . "/n"; 

        //print_r($pageinfo); 
        //exit; 

         
        return $pageinfo; 



/** 
 * 去掉所有的html标记和javascript标记 
 */ 
function replacehtmlandjs($document)  
{  
         $document = trim($document);  
         if (strlen($document) <= 0)  
         {  
          return $document;  
         }  
         $search = array (        
                                            "'<script[^>]*?>.*?
// --></mce:script>'si",  // 去掉 javascript  
                          "'<[///!]*?[^<>]*?>'si",          // 去掉 html 标记  
                          "'[/r/n/s+]'",                // 去掉空白字符  
                          "'&(/w+);'i"              // 替换 html 实体  
                         );                    // 作为 php 代码运行  

         $replace = array ( "", "", "", ""  );  

         return @preg_replace ($search, $replace, $document);  




使用例子

复制代码 代码如下:

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