获取一个网页的meta信息

<?php
//获取一个网页的meta信息
$url       =    "http://www.benxiaohai.com";
$meta   =     get_meta_tags($url);
//在这里还应该注意网页的编码问题,编码如果不一致,可以用iconv进行转码
print_r($meta);
//它将会返回一个标准的一维数组,如果meta信息为空,则返回一个空数组

这个函数是很方便,但是有时候会得到我们想得到更多的信息,这时候就应该再多加一些变化来使用了。下面的一段代码是一个简单的搜索引擎。因为搜索引擎可能只需要meta信息中的keyword和decription信息,所以其它多于的都会过滤掉。

&lt;?php
function get_meta_data($html) {
preg_match_all(
"|&lt;meta[^>]+name=//"([^//"]*)//"[^&gt;]+content=//"([^//"]*)//"[^&gt;]+&gt;|i",  $html, $out,PREG_PATTERN_ORDER);
for ($i=0;$i < count($out[1]);$i++) {
//这里对这个数组进行遍历,取得自己想要的标签
if (strtolower($out[1][$i]) == "keywords") $meta['keywords'] = $out[2][$i];
if (strtolower($out[1][$i]) == "description") $meta['description'] = $out[2][$i];
}
return $meta;
}
?>


接下来的这段代码是一个基于这个的应用

<?php
function getUrlData($url)
{
$result = false;

$contents = getUrlContents($url);
if (isset($contents) && is_string($contents))
{
$title = null;
$metaTags = null;

preg_match('/&lt;title>([^&gt;]*)<//title>/si', $contents, $match );
if (isset($match) && is_array($match) && count($match) &gt; 0)
{
$title = strip_tags($match[1]);
}

preg_match_all('/<[/s]*meta[/s]*name="?' . '([^>"]*)"?[/s]*' . 'content="?([^&gt;"]*)"?[/s]*[//]?[/s]*&gt;/si', $contents, $match);

if (isset($match) && is_array($match) && count($match) == 3)
{
$originals = $match[0];
$names = $match[1];
$values = $match[2];

if (count($originals) == count($names) && count($names) == count($values))
{
$metaTags = array();

for ($i=0, $limiti=count($names); $i < $limiti; $i++)
{
$metaTags[$names[$i]] = array (
'html' => htmlentities($originals[$i]),
'value' =&gt; $values[$i]
);
}
}
}

$result = array (
'title' =&gt; $title,
'metaTags' =&gt; $metaTags
);
}

return $result;
}
function getUrlContents($url, $maximumRedirections = null, $currentRedirection = 0)
{
$result = false;

$contents = @file_get_contents($url);

// Check if we need to go somewhere else

if (isset($contents) && is_string($contents))
{
preg_match_all('/<[/s]*meta[/s]*http-equiv="?REFRESH"?' . '[/s]*content="?[0-9]*;[/s]*URL[/s]*=[/s]*([^>"]*)"?' . '[/s]*[//]?[/s]*&gt;/si', $contents, $match);

if (isset($match) && is_array($match) && count($match) == 2 && count($match[1]) == 1)
{
if (!isset($maximumRedirections) || $currentRedirection < $maximumRedirections)
{
return getUrlContents($match[1][0], $maximumRedirections, ++$currentRedirection);
}

$result = false;
}
else
{
$result = $contents;
}
}

return $contents;
}

$result = getUrlData('http://www.benxiaohai.com');
echo '&lt;pre>'; print_r($result); echo '</pre>';
/****************************************得到的结果如下*******************************
Array
(
    [title] =&gt; 笨小孩心情驿站 - 老天爱笨小孩
    [metaTags] =&gt; Array
        (
            [author] =&gt; Array
                (
                    [html] =&gt; <meta name="author" content="笨å°�å­©å¿�æ��é©¿ç«�" />
                    [value] =&gt; 笨小孩心情驿站
                )

            [description] =&gt; Array
                (
                    [html] =&gt; <meta name="description" content="è��天ç�±ç¬¨å°�å­©" />
                    [value] =&gt; 老天爱笨小孩
                )

            [keywords] =&gt; Array
                (
                    [html] =&gt; <meta name="keywords" content="笨å°�å­©å¿�æ��é©¿ç«�" />
                    [value] =&gt; 笨小孩心情驿站
                )

        )

)
****************************************************************************/
?&gt;

再下面的这段代码,将根据各个标签的长度给出优化建议,可以稍加修改,做出更完美的

<?php
$url  =  "http://www.benxiaohai.com";
$result =  get_meta_tags($url);
if(is_array($result)){
  foreach($result as $key=>$value){
    $key = strtolower($key);
    switch ($key){
      case "keywords":
        $rs["keywords"] = $value;
        break;
      case "description":
        $rs["description"]= $value;
        break;
      case "author":
        $rs["author"]  = $value;
        break;
      case "robots":
        $rs["robots"]  = $value;
        break;
      case "copyright":
        $rs["copyright"] = $value;
        break;
    }
  }
}else
{
  echo "没有任何meta标签";
}
if(empty($rs["robots"])){
  echo "没有Robots标签";
}
if(empty($rs["copyright"])){
  echo "<br>页面没有任何版权信息";
}
if(empty($rs["author"])){
  echo "<br>没有作者信息";
}
if(empty($rs["description"])){
  echo "<br>没有页面描述信息";
}
if(empty($rs["keywords"])){
  echo "<br>没有关键词信息";
}
?&gt;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值