PHP获取网页标题的3种实现方法及代码实例
发布于 2014-12-03 22:18:38 | 134 次阅读 | 评论: 0 | 来源: 网友投递
PHP开源脚本语言PHP(外文名: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言。语法吸收了C语言、Java和Perl的特点,入门门槛较低,易于学习,使用广泛,主要适用于Web开发领域。PHP的文件后缀名为php。
本文为大家提供的是PHP获取网页标题的3种实现方法及代码实例.分别使用CURL、file()函数、file_get_contents实现,需要的朋友可以参考下
一、推荐方法 CURL获取
$c = curl_init();
$url = 'www.xxx.net';
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("/
(.*)/i",$data, $title);echo $title[1];
?>
二、使用file()函数
$lines_array = file('http://www.xxx.net/');
$lines_string = implode('', $lines_array);
$pos = strpos($lines_string,'utf-8');
if($pos===false){$lines_string = iconv("gbk","utf-8",$lines_string);}
eregi("
(.*)", $lines_string, $title);echo $title[1];
?>
三、使用file_get_contents
$content=file_get_contents("http://www.xxx.net/");
$pos = strpos($content,'utf-8');
if($pos===false){$content = iconv("gbk","utf-8",$content);}
$postb=strpos($content,'
')+7;$poste=strpos($content,'
');$length=$poste-$postb;
echo substr($content,$postb,$length);
?>
相关阅读:
PHP获取网页标题的3种实现方法及代码实例
PHP跳转页面的几种实现方法详解
php自动加载的两种实现方法
php文件上传的两种实现方法
PHP连接MYSQL数据库的3种常用方法
PHP 获取远程文件大小的3种解决方法
PHP CURL或file_get_contents获取网页标题的代码及两者效率的稳定性问题
PHP生成短网址的3种方法
php获取get参数的几种方法总结
php获取网页标题和内容函数(不包含html标签)
php 使用file_get_contents抓取Gzip网页乱码的三种解决方法
PHP中将ip地址转成十进制数的两种实现方法