1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file contains the keyhighlighter class that highlight the chosen keyword in the current output buffer. |
4
|
|
|
* |
5
|
|
|
* @package keyhighlighter |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* keyhighlighter class |
10
|
|
|
* |
11
|
|
|
* This class highlight the chosen keywords in the current output buffer |
12
|
|
|
* |
13
|
|
|
* @package keyhighlighter |
14
|
|
|
* @author Setec Astronomy |
15
|
|
|
* @abstract Highlight specific keywords. |
16
|
|
|
* @copyright 2004 |
17
|
|
|
* @example sample.php A sample code. |
18
|
|
|
* @link http://setecastronomy.stufftoread.com |
19
|
|
|
*/ |
20
|
|
|
class SmartpartnerKeyhighlighter |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @access private |
24
|
|
|
*/ |
25
|
|
|
public $preg_keywords = ''; |
26
|
|
|
/** |
27
|
|
|
* @access private |
28
|
|
|
*/ |
29
|
|
|
public $keywords = ''; |
30
|
|
|
/** |
31
|
|
|
* @access private |
32
|
|
|
*/ |
33
|
|
|
public $singlewords = false; |
34
|
|
|
/** |
35
|
|
|
* @access private |
36
|
|
|
*/ |
37
|
|
|
public $replace_callback = null; |
38
|
|
|
|
39
|
|
|
public $content; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Main constructor |
43
|
|
|
* |
44
|
|
|
* This is the main constructor of keyhighlighter class. <br> |
45
|
|
|
* It's the only public method of the class. |
46
|
|
|
* @param string $keywords the keywords you want to highlight |
47
|
|
|
* @param boolean $singlewords specify if it has to highlight also the single words. |
48
|
|
|
* @param callback $replace_callback a custom callback for keyword highlight. |
49
|
|
|
* <code> |
50
|
|
|
* <?php |
51
|
|
|
* require ('keyhighlighter.class.php'); |
52
|
|
|
* |
53
|
|
|
* function my_highlighter ($matches) { |
54
|
|
|
* return '<span style="font-weight: bolder; color: #FF0000;">' . $matches[0] . '</span>'; |
55
|
|
|
* } |
56
|
|
|
* |
57
|
|
|
* new keyhighlighter ('W3C', false, 'my_highlighter'); |
58
|
|
|
* readfile ('http://www.w3c.org/'); |
59
|
|
|
* ?> |
60
|
|
|
* </code> |
61
|
|
|
*/ |
62
|
|
|
// public function __construct () |
|
|
|
|
63
|
|
|
public function __construct($keywords, $singlewords = false, $replace_callback = null) |
64
|
|
|
{ |
65
|
|
|
$this->keywords = $keywords; |
66
|
|
|
$this->singlewords = $singlewords; |
67
|
|
|
$this->replace_callback = $replace_callback; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @access private |
72
|
|
|
* @param $replace_matches |
73
|
|
|
* @return mixed |
74
|
|
|
*/ |
75
|
|
|
public function replace($replace_matches) |
76
|
|
|
{ |
77
|
|
|
$patterns = array(); |
78
|
|
|
if ($this->singlewords) { |
79
|
|
|
$keywords = explode(' ', $this->preg_keywords); |
80
|
|
|
foreach ($keywords as $keyword) { |
81
|
|
|
$patterns[] = '/(?' . '>' . $keyword . '+)/si'; |
82
|
|
|
} |
83
|
|
|
} else { |
84
|
|
|
$patterns[] = '/(?' . '>' . $this->preg_keywords . '+)/si'; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$result = $replace_matches[0]; |
88
|
|
|
|
89
|
|
|
foreach ($patterns as $pattern) { |
90
|
|
|
if (null !== $this->replace_callback) { |
91
|
|
|
$result = preg_replace_callback($pattern, $this->replace_callback, $result); |
92
|
|
|
} else { |
93
|
|
|
$result = preg_replace($pattern, '<span class="highlightedkey">\\0</span>', $result); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $result; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @access private |
102
|
|
|
* @param $buffer |
103
|
|
|
* @return mixed|string |
104
|
|
|
*/ |
105
|
|
|
public function highlight($buffer) |
106
|
|
|
{ |
107
|
|
|
$buffer = '>' . $buffer . '<'; |
108
|
|
|
$this->preg_keywords = preg_replace('/[^\w ]/si', '', $this->keywords); |
109
|
|
|
$buffer = preg_replace_callback("/(\>(((?" . ">[^><]+)|(?R))*)\<)/is", array(&$this, 'replace'), $buffer); |
110
|
|
|
$buffer = substr($buffer, 1, -1); |
111
|
|
|
|
112
|
|
|
return $buffer; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.