root/trunk/plugins/searchhl.php

Revision 680, 2.4 KB (checked in by michiel, 14 months ago)

added first version of a searchengine search keyword highlighter plugin.
Only works with google and only skips stuff inside <a*> tags.
Needs a lot of work before it can be included in a release.

Line 
1<?php
2Class searchhl extends MvBlog_plugin implements MvBlog_pluginiface {
3    /* variables */
4    public $name    = "SearchHighlight";
5    public $author  = "Michiel van Baak";
6    public $license = "GPL";
7    public $website = "http://www.mvblog.org";
8    public $description = "Plugin to highlight the words typed in at searchengine.";
9
10    private $_mvblog;
11
12    /* methods */
13    public function __construct(&$mvblog) {
14        $this->addHook("text_output", "highlightsearch");
15        $this->addHook("css_output" , "searchhlCSS");
16        $this->_mvblog =& $mvblog;
17    }
18
19    public function activate() {
20        /* get data, do whatever is needed on plugin activation */
21    }
22
23    public function deactivate() {
24    }
25
26    public function highlightsearch($text) {
27        if (array_key_exists("HTTP_REFERER", $_SERVER) && $_SERVER["HTTP_REFERER"]) {
28            //this is for google only right now.
29            //TODO: implement some searchengine abstraction
30            preg_match("/[\?|&]q=(.*)[$|&]/", $_SERVER["HTTP_REFERER"], $matches);
31            if (array_key_exists(1, $matches) && $matches[1]) {
32                //some stuff in the post has to be left alone. replace them with markers so we can put them back later
33                preg_match_all("/<a[^>](.*?)>/si", $text, $txtmatches);
34                foreach ($txtmatches[0] as $txtmatchk=>$txtmatchv) {
35                    $text = str_replace($txtmatchv, "##".$txtmatchk."##", $text);
36                }
37                $searchwords = explode("+", $matches[1]);
38                foreach ($searchwords as $k=>$v) {
39                    $text = str_replace($v, "<span class=\"searchhl_".$k."\">".$v."</span>", $text);
40                }
41                //put the skipped stuff back in the buffer
42                foreach ($txtmatches[0] as $txtmatchk=>$txtmatchv) {
43                    $text = str_replace("##".$txtmatchk."##", $txtmatchv, $text);
44                }
45            }
46        }
47        return $text;
48    }
49
50    public function searchhlCSS($data) {
51        $output  = $data;
52        $output .= "\t<style type=\"text/css\">\n";
53        $output .= "\t\t.searchhl_0 { background-color: #fcc; }\n";
54        $output .= "\t\t.searchhl_1 { background-color: #ff0; }\n";
55        $output .= "\t\t.searchhl_2 { background-color: #f0f; }\n";
56        $output .= "\t\t.searchhl_3 { background-color: #0ff; }\n";
57        $output .= "\t\t.searchhl_4 { background-color:#FF9900; }\n";
58        $output .= "\t\t.searchhl_5 { background-color:#99FF66; }\n";
59        $output .= "\t\t.searchhl_6 { background-color:#99CCFF; }\n";
60        $output .= "\t\t.searchhl_7 { background-color:#FF3333; }\n";
61        $output .= "\t\t.searchhl_8 { background-color:#FF99FF; }\n";
62        $output .= "\t\t.searchhl_9 { background-color:#FFFFCC; }\n";
63        $output .= "\t</style>\n";
64        return $output;
65    }
66}
67?>
Note: See TracBrowser for help on using the browser.