Discuz教程网

用PHP获取网页的Meta信息

[复制链接]
authicon dly 发表于 2012-10-13 22:26:03 | 显示全部楼层 |阅读模式
一个网页的Meta信息虽然不能在页面中直接显示,但是对于搜索引擎来说还是很管用的,特别是几个重要的标签,比如Keyword和description等。接触过SEO的人都应该很明白。

       在PHP中有现成的函数get_meta_tags可以直接获取到一个网页所有的meta信息。它的用法比较简单,如下所述:

  1. <?php
  2. //获取一个网页的meta信息
  3. $url = "http://www.benxiaohai.com";
  4. $meta = get_meta_tags($url);
  5. //在这里还应该注意网页的编码问题,编码如果不一致,可以用iconv进行转码
  6. print_r($meta);
  7. //它将会返回一个标准的一维数组,如果meta信息为空,则返回一个空数组 这个函数是很方便,但是有时候会得到我们想得到更多的信息,这时候就应该再多加一些变化来使用了。下面的一段代码是一个简单的搜索引擎。因为搜索引擎可能只需要meta信息中的keyword和decription信息,所以其它多于的都会过滤掉。

  8. <?php
  9. function get_meta_data($html) {
  10. preg_match_all(
  11. "|<meta[^>]+name=\"([^\"]*)\"[^>]+content=\"([^\"]*)\"[^>]+>|i", $html, $out,PREG_PATTERN_ORDER);
  12. for ($i=0;$i < count($out[1]);$i++) {
  13. //这里对这个数组进行遍历,取得自己想要的标签
  14. if (strtolower($out[1][$i]) == "keywords") $meta['keywords'] = $out[2][$i];
  15. if (strtolower($out[1][$i]) == "description") $meta['description'] = $out[2][$i];
  16. }
  17. return $meta;
  18. }
  19. ?>
复制代码
接下来的这段代码是一个基于这个的应用


  1. <?php
  2. function getUrlData($url)
  3. {
  4. $result = false;

  5. $contents = getUrlContents($url);
  6. if (isset($contents) && is_string($contents))
  7. {
  8. $title = null;
  9. $metaTags = null;

  10. preg_match('/<title>([^>]*)<\/title>/si', $contents, $match );
  11. if (isset($match) && is_array($match) && count($match) > 0)
  12. {
  13. $title = strip_tags($match[1]);
  14. }

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

  16. if (isset($match) && is_array($match) && count($match) == 3)
  17. {
  18. $originals = $match[0];
  19. $names = $match[1];
  20. $values = $match[2];

  21. if (count($originals) == count($names) && count($names) == count($values))
  22. {
  23. $metaTags = array();

  24. for ($i=0, $limiti=count($names); $i < $limiti; $i++)
  25. {
  26. $metaTags[$names[$i]] = array (
  27. 'html' => htmlentities($originals[$i]),
  28. 'value' => $values[$i]
  29. );
  30. }
  31. }
  32. }
  33. $result = array (
  34. 'title' => $title,
  35. 'metaTags' => $metaTags
  36. );
  37. }

  38. return $result;
  39. }
  40. function getUrlContents($url, $maximumRedirections = null, $currentRedirection = 0)
  41. {
  42. $result = false;

  43. $contents = @file_get_contents($url);

  44. // Check if we need to go somewhere else

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

  48. if (isset($match) && is_array($match) && count($match) == 2 && count($match[1]) == 1)
  49. {
  50. if (!isset($maximumRedirections) || $currentRedirection < $maximumRedirections)
  51. {
  52. return getUrlContents($match[1][0], $maximumRedirections, ++$currentRedirection);
  53. }

  54. $result = false;
  55. }
  56. else
  57. {
  58. $result = $contents;
  59. }
  60. }

  61. return $contents;
  62. }

  63. $result = getUrlData('http://www.benxiaohai.com');
  64. echo '<pre>'; print_r($result); echo '</pre>';
  65. /****************************************得到的结果如下*******************************
  66. Array
  67. (
  68.      [title] => 笨小孩心情驿站 - 老天爱笨小孩
  69.      [metaTags] => Array
  70.          (
  71.              [author] => Array
  72.                  (
  73.                      [html] => <meta name="author" content="&ccedil;&not;¨&aring;°&#65533;&aring;&shy;&copy;&aring;&iquest;&#65533;&aelig;&#65533;&#65533;é&copy;&iquest;&ccedil;&laquo;&#65533;" />
  74.                      [value] => 笨小孩心情驿站
  75.                  )

  76.              [description] => Array
  77.                  (
  78.                      [html] => <meta name="description" content="è&#65533;&#65533;&aring;¤&copy;&ccedil;&#65533;±&ccedil;&not;¨&aring;°&#65533;&aring;&shy;&copy;" />
  79.                      [value] => 老天爱笨小孩
  80.                  )

  81.              [keywords] => Array
  82.                  (
  83.                      [html] => <meta name="keywords" content="&ccedil;&not;¨&aring;°&#65533;&aring;&shy;&copy;&aring;&iquest;&#65533;&aelig;&#65533;&#65533;é&copy;&iquest;&ccedil;&laquo;&#65533;" />
  84.                      [value] => 笨小孩心情驿站
  85.                  )

  86.          )

  87. )
  88. ****************************************************************************/
  89. ?>
复制代码

再下面的这段代码,将根据各个标签的长度给出优化建议,可以稍加修改,做出更完美的
  1. <?php
  2. $url   =   "http://www.benxiaohai.com";
  3. $result =   get_meta_tags($url);
  4. if(is_array($result)){
  5.    foreach($result as $key=>$value){
  6.      $key = strtolower($key);
  7.      switch ($key){
  8.        case "keywords":
  9.          $rs["keywords"] = $value;
  10.          break;
  11.        case "description":
  12.          $rs["description"]= $value;
  13.          break;
  14.        case "author":
  15.          $rs["author"]   = $value;
  16.          break;
  17.        case "robots":
  18.          $rs["robots"]   = $value;
  19.          break;
  20.        case "copyright":
  21.          $rs["copyright"] = $value;
  22.          break;
  23.      }
  24.    }
  25. }else
  26. {
  27.    echo "没有任何meta标签";
  28. }
  29. if(empty($rs["robots"])){
  30.    echo "没有Robots标签";
  31. }
  32. if(empty($rs["copyright"])){
  33.    echo "<br>页面没有任何版权信息";
  34. }
  35. if(empty($rs["author"])){
  36.    echo "<br>没有作者信息";
  37. }
  38. if(empty($rs["description"])){
  39.    echo "<br>没有页面描述信息";
  40. }
  41. if(empty($rs["keywords"])){
  42.    echo "<br>没有关键词信息";
  43. }
  44. ?>
复制代码



上一篇:首页N格忘记网站ID
下一篇:Discuz X2.5去掉最后发表,删除主题 贴数,显示版块简介
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 07:22

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表