标签:== 句柄 执行 headers .com title ini 文字 output
<?php
$url = ‘http://www.k7wan.com‘;
echo getTitle_web_curl($url);
function getTitle_web_curl($url){
$title = ‘‘;
$ch = curl_init();
//设置选项,包括URL
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
//执行并获取HTML文档内容
$output = curl_exec($ch);
//释放curl句柄
curl_close($ch);
// 解析 HTML 的 <head> 区段
preg_match("/<head.*>(.*)<\/head>/smUi",$output, $htmlHeaders);
if(!count($htmlHeaders)){
$title = "无法解析数据中的 <head> 区段";
}
// 取得 <head> 中 meta 设置的编码格式<meta charset="gb2312">
if(preg_match(‘/charset="(.*)"/‘,$htmlHeaders[1], $results)){
$charset = $results[1];
}else{
$charset = "None";
}
// 取得 <title> 中的文字
if(preg_match("/<title>(.*)<\/title>/Ui",$htmlHeaders[1], $htmlTitles)){
if(!count($htmlTitles)){
$title = "无法解析 <title> 的内容";
exit;
}
// 将 <title> 的文字编码格式转成 UTF-8
if($charset == "None"){
$title=$htmlTitles[1];
}else{
$title=iconv($charset, "UTF-8", $htmlTitles[1]);
}
}
return $title;
}
标签:== 句柄 执行 headers .com title ini 文字 output
原文地址:http://www.cnblogs.com/as3lib/p/6829208.html