今天给大家带来的是php获取网页mate内容的代码
(标题,关键词,描述)
代码如下:
<?php
$site = "https://www.likinming.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('/<TITLE>([\w\W]*?)<\/TITLE>/si', $data, $matches);
if (!empty($matches[1])) {
$meta['title'] = $matches[1];
}
#Keywords
preg_match('/<META\s+name="keywords"\s+content="([\w\W]*?)"/si', $data, $matches);
if (empty($matches[1])) {
preg_match("/<META\s+name='keywords'\s+content='([\w\W]*?)'/si", $data, $matches);
}
if (empty($matches[1])) {
preg_match('/<META\s+content="([\w\W]*?)"\s+name="keywords"/si', $data, $matches);
}
if (empty($matches[1])) {
preg_match('/<META\s+http-equiv="keywords"\s+content="([\w\W]*?)"/si', $data, $matches);
}
if (!empty($matches[1])) {
$meta['keywords'] = $matches[1];
}
#Description
preg_match('/<META\s+name="description"\s+content="([\w\W]*?)"/si', $data, $matches);
if (empty($matches[1])) {
preg_match("/<META\s+name='description'\s+content='([\w\W]*?)'/si", $data, $matches);
}
if (empty($matches[1])) {
preg_match('/<META\s+content="([\w\W]*?)"\s+name="description"/si', $data, $matches);
}
if (empty($matches[1])) {
preg_match('/<META\s+http-equiv="description"\s+content="([\w\W]*?)"/si', $data, $matches);
}
if (!empty($matches[1])) {
$meta['description'] = $matches[1];
}
}
return $meta;
}
?>
文章作者:博客之家
文章標題:PHP抓取网页mate内容代码
文章鏈接:https://www.likinming.com/post-2759.html
本站所有文章除特別聲明外,均採用 CC BY-NC-SA 4.0 許可協議,轉載請註明來自博客之家 !
文章標題:PHP抓取网页mate内容代码
文章鏈接:https://www.likinming.com/post-2759.html
本站所有文章除特別聲明外,均採用 CC BY-NC-SA 4.0 許可協議,轉載請註明來自博客之家 !

发表评论