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>
<?php /* ********************************************************* * AutoTag script *  Automatically places HTML tags around specified terms * Copyright (C) 2004 GreyWyvern * * This program may be distributed under the terms of the GPL *   - http://www.gnu.org/licenses/gpl.txt * * Note: This program assumes you already have a webpage setup * where your site is written to the output buffer before being * sent to the client.  If you have no idea what the ob_start() * function in PHP does, you need to learn output buffering before * attempting to use this script. * * This script will search for certain terms in your output buffer * and enclose them within a set of opening and closing HTML tags * which you can define.  Only terms within the <body> but outside * HTML tags are affected.  As well, only the first instance of * each term will have the autoTag applied. * * Just run the code between Begin Setup and End Setup for each * page to load the CSV file, then apply the "if ($aList)..." * block to the output buffer string before you send it away. * * The CSV format is simple, where each line specifies a tag.  The * first entry is the text to search for, the next is the text to * insert in the title attribute of the tag.  For an <abbr> tag, * this would be the full name of the abbreviation.  To prevent * insertion of a title attribute, just leave the entry blank. The * next two entries are the opening and closing tag name * respectively.  You can be quite creative with the values which * go here: *                   ,abbr,abbr = <abbr></abbr> *             ,acronym,acronym = <acronym></acronym> * ,span class="highlight",span = <span class="highlight"></span> * * The last value is the replacement limit.  Usually you want only * the first acronym or abbreviation on your page to be tagged. * For things like highlighting google search terms (explained * later), you'd rather have all matching terms tagged.  The last * value then is a numerical limit to the number of matches which * will get tagged.  A value of -1 will cause all terms to be * matched. * * Make sure that if any of your CSV entries contains a comma, the * entire entry is enclosed in quotes, otherwise the script will * think the comma is actually the next point to divide the values. * Think "Comma Separated Values" and you'll always remember. * * You can also add dynamic autoTag events (ones that change from * page to page and visit to visit) by adding your own elements to * the $aList array.  A description showing how to highlight Google * referrer terms is provided. * * See the inline comments and http://www.greywyvern.com/php.php * for more info. *************************************************************** */   /* ***** Begin Setup ****************************************** */ // Define the autoTag class class autoTag {   var $match;   var $title;   var $elmnt;   function add ($match, $title, $tagOp, $tagCl, $limit = -1) {     $this->match = $match;     $this->title = $title;     $this->tagOp = $tagOp;     $this->tagCl = $tagCl;     $this->limit = $limit;   } }   // Create the autoTag array for each line of the csv file. //  Make sure the autotag.csv file exists! $csv = fopen("autotag.csv", "r"); $inc = 0; if ($csv) {   while ($line = fgetcsv($csv, 1024)) {     $aList[$inc] = new autoTag;     $aList[$inc++]->add($line[0], $line[1], $line[2], $line[3], $line[4]);   }   fclose($csv); } /* ***** End Setup ******************************************** */     /* ***** Add dynamic autoTags - Google referrer example ******* */ // Extract the search terms used to get to this page if (isset($_SERVER['HTTP_REFERER'])) {   $ref = parse_url($_SERVER['HTTP_REFERER']);   if (strpos($ref['host'], ".google.") !== false && isset($ref['query'])) {     preg_match("/^.*q=([^&]*)(&.*)?$/i", $ref['query'], $match);     if ($match[1]) {       $match[1] = urldecode($match[1]);         // keep text within quotes together       preg_match_all("//"(.*?)/"/", $match[1], $quotes);       foreach ($quotes[1] as $quot) $terms[] = $quot;         $match[1] = preg_replace("//".*?/"/", "", $match[1]);       $match[1] = preg_replace("//"/", "", $match[1]);       $match[1] = preg_replace("/  /", " ", $match[1]);         $terms = array_merge($terms, explode(" ", $match[1]));         // Create new autoTag array elements       $inc = count($aList);         foreach ($terms as $term) {         if (trim($term)) {           $aList[$inc] = new autoTag;           $aList[$inc++]->add($term, "", "span class=/"highlight/"", "span", -1);         }       }     }   } } /* ***** Add dynamic autoTags - End *************************** */     function bufferEdit($input) {   global $aList;     /* ***** Begin autoTagging ********************************** */   // $input is your compiled output buffer - see instructions   // if you use a different variable name, make sure to change it   //  in the preg_match() function call and the last line here.   if ($aList) {     preg_match("/(^.*<body.*?>)(.*)(<//body>.*)$/s", $input, $part);     foreach ($aList as $at) {       $title = ($at->title) ? " title=/"{$at->title}/"" : "";       $part[2] = preg_replace("/(>[^<]*?/b)(".preg_quote($at->match).")/b/si", "$1<{$at->tagOp}$title>$2</{$at->tagCl}>", $part[2], $at->limit);     }   }   $input = $part[1].$part[2].$part[3];   /* ***** End autoTagging ************************************ */     return $input; }   ob_start(); ?><html> <head>   <title>HTML Page</title>   <style type="text/css">   body {   background-color:#ffffff; }   /* ***** Google Highlight ***** */ .highlight {   background-color: #ffff99; }     </style> </head> <body>   Content   </body> </html><?php $page = ob_get_contents(); ob_end_clean(); echo bufferEdit($page);   ?>
發佈了17 篇原創文章 · 獲贊 0 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章