加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

php获取网页meta信息(包括title、keywords、description)的两种方法

发布时间:2020-05-30 18:57:36 所属栏目:PHP 来源:互联网
导读:在网页采集过程中,我们需要获取一个网站的meta信息,如title、keywords、description等,本文章向大家介绍两种方法获取网站的meta信息,第一种方法是使用get_meta_tags函数,第二种方法是使用正则表达式匹配的方法获

使用get_meta_tags函数获取meta信息

比如我们要获取http://www.taobao.com这个网页的meta信息,可以直接使用php内置函数get_meta_tags获取,代码如下:

$meta_tags = get_meta_tags("http://www.taobao.com");

print_r($meta_tags);

?>

结果输出:

Array

(

[renderer] => webkit

[spm-id] => a21bo

[description] => 淘宝网 - 亚洲最大、最安全的网上交易平台,提供各类服饰、美容、家居、数码、话费/点卡充值… 8亿优质特价商品,同时提供担保交易(先收货后付款)、先行赔付、假一赔三、七天无理由退换货、数码免费维修等安全交易保障服务,让你全面安心享受网上购物乐趣!

[keyword] =>

)

使用正则表达式获取meta信息

PHP代码如下:

$site = "http://www.manongjc.com";

$content = get_sitemeta($site);

print_r($content);

/** 获取META信息 */

function get_sitemeta($url) {

$data = file_get_contents($url);

$meta = array();

if (!empty($data)) {

#Title

preg_match('/([wW]*?)/si',$data,$matches);

if (!empty($matches[1])) {

$meta['title'] = $matches[1];

}

#Keywords

preg_match('/

if (empty($matches[1])) {

preg_match("/

}

if (empty($matches[1])) {

preg_match('/

}

if (empty($matches[1])) {

preg_match('/

}

if (!empty($matches[1])) {

$meta['keywords'] = $matches[1];

}

#Description

preg_match('/

if (empty($matches[1])) {

preg_match("/

}

if (empty($matches[1])) {

preg_match('/

}

if (empty($matches[1])) {

preg_match('/

}

if (!empty($matches[1])) {

$meta['description'] = $matches[1];

}

}

return $meta;

}

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读