PHP获取网页标题的3种实现方法代码实例

前端之家收集整理的这篇文章主要介绍了PHP获取网页标题的3种实现方法代码实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一、推荐方法 CURL获取

PHP
$c = curl_init();
$url = 'www.jb51.cc';
curl_setopt($c,CURLOPT_URL,$url);
curl_setopt($c,CURLOPT_RETURNTRANSFER,1);
$data = curl_exec($c);
curl_close($c);
$pos = strpos($data,'utf-8');
if($pos===false){$data = iconv("gbk","utf-8",$data);}
preg_match("/(.*)<\/title>/i",$data,$title);<BR>echo $title[1];<BR>?></P> <P>二、使用file()<a href="https://www.jb51.cc/tag/hanshu/" target="_blank" class="keywords">函数</a></P> <P><?<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a><BR>$lines_array = file('//www.jb51.cc/');<BR>$lines_string = implode('',$lines_array); <BR>$pos = strpos($lines_string,'utf-8');<BR>if($pos===false){$lines_string = iconv("gbk",$lines_string);}<BR>eregi("<title>(.*)",$lines_string,$title);
echo $title[1];
?>

三、使用file_get_contents

PHP
$content=file_get_contents("//www.jb51.cc/");
$pos = strpos($content,'utf-8');
if($pos===false){$content = iconv("gbk",$content);}
$postb=strpos($content,'')+7;<BR>$poste=strpos($content,'');
$length=$poste-$postb;
echo substr($content,$postb,$length);
?>

原文链接:https://www.f2er.com/php/24819.html

猜你在找的PHP相关文章