加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-12 20:02:48 所属栏目:PHP教程 来源:网络整理
导读:div class="codetitle" a style="CURSOR: pointer" data="43374" class="copybut" id="copybut43374" onclick="doCopy('code43374')" 代码如下: div class="codebody" id="code43374" function getPageContent($url) { //$url='http://www.ttphp.com; $pagei

<div class="codetitle"><a style="CURSOR: pointer" data="43374" class="copybut" id="copybut43374" onclick="doCopy('code43374')"> 代码如下:<div class="codebody" id="code43374">
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,CURLOPT_TIMEOUT,8);
curl_setopt($ch,CURLOPT_FILETIME,CURLOPT_FOLLOWLOCATION,1);
//curl_setopt($ch,CURLOPT_HEADER,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',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 </