AutoTag script1.0

Would you love to make your code complete with <abbr> and <acronym> tags but hate always inserting them manually for every page you generate on your PHP driven site? Wouldn't you like a way for a script to go through your content on each page and do it automatically and limit the number of times a certain term gets tagged? Or would you like a way to highlight the search terms on your page for people arriving from Google?

The autoTag script will take an output buffer string HTML page, search for certain terms and wrap them in HTML tags you can specify with an easy to create .csv file. You can even include custom tagging by adding array elements to search for. An example displaying Google search term highlighting is provided; when users arrive at your site via Google, all the terms they searched for are tagged and highlighted on your page!

PHP: <script type="text/javascript"> </script> 选择 缩进 <script type="text/javascript">AK47[id].writeDiv();</script>
  1. <?php /* *********************************************************
  2. * AutoTag script
  3. *  Automatically places HTML tags around specified terms
  4. * Copyright (C) 2004 GreyWyvern
  5. *
  6. * This program may be distributed under the terms of the GPL
  7. *   - http://www.gnu.org/licenses/gpl.txt
  8. *
  9. * Note: This program assumes you already have a webpage setup
  10. * where your site is written to the output buffer before being
  11. * sent to the client.  If you have no idea what the ob_start()
  12. * function in PHP does, you need to learn output buffering before
  13. * attempting to use this script.
  14. *
  15. * This script will search for certain terms in your output buffer
  16. * and enclose them within a set of opening and closing HTML tags
  17. * which you can define.  Only terms within the <body> but outside
  18. * HTML tags are affected.  As well, only the first instance of
  19. * each term will have the autoTag applied.
  20. *
  21. * Just run the code between Begin Setup and End Setup for each
  22. * page to load the CSV file, then apply the "if ($aList)..."
  23. * block to the output buffer string before you send it away.
  24. *
  25. * The CSV format is simple, where each line specifies a tag.  The
  26. * first entry is the text to search for, the next is the text to
  27. * insert in the title attribute of the tag.  For an <abbr> tag,
  28. * this would be the full name of the abbreviation.  To prevent
  29. * insertion of a title attribute, just leave the entry blank. The
  30. * next two entries are the opening and closing tag name
  31. * respectively.  You can be quite creative with the values which
  32. * go here:
  33. *                   ,abbr,abbr = <abbr></abbr>
  34. *             ,acronym,acronym = <acronym></acronym>
  35. * ,span class="highlight",span = <span class="highlight"></span>
  36. *
  37. * The last value is the replacement limit.  Usually you want only
  38. * the first acronym or abbreviation on your page to be tagged.
  39. * For things like highlighting google search terms (explained
  40. * later), you'd rather have all matching terms tagged.  The last
  41. * value then is a numerical limit to the number of matches which
  42. * will get tagged.  A value of -1 will cause all terms to be
  43. * matched.
  44. *
  45. * Make sure that if any of your CSV entries contains a comma, the
  46. * entire entry is enclosed in quotes, otherwise the script will
  47. * think the comma is actually the next point to divide the values.
  48. * Think "Comma Separated Values" and you'll always remember.
  49. *
  50. * You can also add dynamic autoTag events (ones that change from
  51. * page to page and visit to visit) by adding your own elements to
  52. * the $aList array.  A description showing how to highlight Google
  53. * referrer terms is provided.
  54. *
  55. * See the inline comments and http://www.greywyvern.com/php.php
  56. * for more info.
  57. *************************************************************** */
  58.  
  59. /* ***** Begin Setup ****************************************** */
  60. // Define the autoTag class
  61. class autoTag {
  62.   var $match;
  63.   var $title;
  64.   var $elmnt;
  65.   function add ($match, $title, $tagOp, $tagCl, $limit = -1) {
  66.     $this->match = $match;
  67.     $this->title = $title;
  68.     $this->tagOp = $tagOp;
  69.     $this->tagCl = $tagCl;
  70.     $this->limit = $limit;
  71.   }
  72. }
  73.  
  74. // Create the autoTag array for each line of the csv file.
  75. //  Make sure the autotag.csv file exists!
  76. $csv = fopen("autotag.csv", "r");
  77. $inc = 0;
  78. if ($csv) {
  79.   while ($line = fgetcsv($csv, 1024)) {
  80.     $aList[$inc] = new autoTag;
  81.     $aList[$inc++]->add($line[0], $line[1], $line[2], $line[3], $line[4]);
  82.   }
  83.   fclose($csv);
  84. }
  85. /* ***** End Setup ******************************************** */
  86.  
  87.  
  88. /* ***** Add dynamic autoTags - Google referrer example ******* */
  89. // Extract the search terms used to get to this page
  90. if (isset($_SERVER['HTTP_REFERER'])) {
  91.   $ref = parse_url($_SERVER['HTTP_REFERER']);
  92.   if (strpos($ref['host'], ".google.") !== false && isset($ref['query'])) {
  93.     preg_match("/^.*q=([^&]*)(&.*)?$/i", $ref['query'], $match);
  94.     if ($match[1]) {
  95.       $match[1] = urldecode($match[1]);
  96.  
  97.       // keep text within quotes together
  98.       preg_match_all("//"(.*?)/"/", $match[1], $quotes);
  99.       foreach ($quotes[1] as $quot) $terms[] = $quot;
  100.  
  101.       $match[1] = preg_replace("//".*?/"/", "", $match[1]);
  102.       $match[1] = preg_replace("//"/", "", $match[1]);
  103.       $match[1] = preg_replace("/  /", " ", $match[1]);
  104.  
  105.       $terms = array_merge($terms, explode(" ", $match[1]));
  106.  
  107.       // Create new autoTag array elements
  108.       $inc = count($aList);
  109.  
  110.       foreach ($terms as $term) {
  111.         if (trim($term)) {
  112.           $aList[$inc] = new autoTag;
  113.           $aList[$inc++]->add($term, "", "span class=/"highlight/"", "span", -1);
  114.         }
  115.       }
  116.     }
  117.   }
  118. }
  119. /* ***** Add dynamic autoTags - End *************************** */
  120.  
  121.  
  122. function bufferEdit($input) {
  123.   global $aList;
  124.  
  125.   /* ***** Begin autoTagging ********************************** */
  126.   // $input is your compiled output buffer - see instructions
  127.   // if you use a different variable name, make sure to change it
  128.   //  in the preg_match() function call and the last line here.
  129.   if ($aList) {
  130.     preg_match("/(^.*<body.*?>)(.*)(<//body>.*)$/s", $input, $part);
  131.     foreach ($aList as $at) {
  132.       $title = ($at->title) ? " title=/"{$at->title}/"" : "";
  133.       $part[2] = preg_replace("/(>[^<]*?/b)(".preg_quote($at->match).")/b/si", "$1<{$at->tagOp}$title>$2</{$at->tagCl}>", $part[2], $at->limit);
  134.     }
  135.   }
  136.   $input = $part[1].$part[2].$part[3];
  137.   /* ***** End autoTagging ************************************ */
  138.  
  139.   return $input;
  140. }
  141.  
  142. ob_start(); ?><html>
  143. <head>
  144.   <title>HTML Page</title>
  145.   <style type="text/css">
  146.  
  147. body {
  148.   background-color:#ffffff;
  149. }
  150.  
  151. /* ***** Google Highlight ***** */
  152. .highlight {
  153.   background-color: #ffff99;
  154. }
  155.  
  156.   </style>
  157. </head>
  158. <body>
  159.  
  160. Content
  161.  
  162. </body>
  163. </html><?php
  164. $page = ob_get_contents();
  165. ob_end_clean();
  166. echo bufferEdit($page);
  167.  
  168. ?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值