|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace XoopsModules\News; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* This file contains the Keyhighlighter class that highlight the chosen keyword in the current output buffer. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Keyhighlighter class |
|
11
|
|
|
* |
|
12
|
|
|
* This class highlight the chosen keywords in the current output buffer |
|
13
|
|
|
* |
|
14
|
|
|
* @author Setec Astronomy |
|
15
|
|
|
* @abstract Highlight specific keywords. |
|
16
|
|
|
* @copyright 2004 |
|
17
|
|
|
* @example sample.php A sample code. |
|
18
|
|
|
* @link https://setecastronomy.stufftoread.com |
|
19
|
|
|
*/ |
|
20
|
|
|
class Keyhighlighter |
|
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
|
|
|
public $content; |
|
39
|
|
|
/** |
|
40
|
|
|
* Main constructor |
|
41
|
|
|
* |
|
42
|
|
|
* This is the main constructor of Keyhighlighter class. <br> |
|
43
|
|
|
* It's the only public method of the class. |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $keywords the keywords you want to highlight |
|
46
|
|
|
* @param bool $singlewords specify if it has to highlight also the single words. |
|
47
|
|
|
* @param callback $replace_callback a custom callback for keyword highlight. |
|
48
|
|
|
* <code> |
|
49
|
|
|
* <?php |
|
50
|
|
|
* require_once ('Keyhighlighter.class.php'); |
|
51
|
|
|
* |
|
52
|
|
|
* function my_highlighter ($matches) { |
|
53
|
|
|
* return '<span style="font-weight: bolder; color: #FF0000;">' . $matches[0] . '</span>'; |
|
54
|
|
|
* } |
|
55
|
|
|
* |
|
56
|
|
|
* new Keyhighlighter ('W3C', false, 'my_highlighter'); |
|
57
|
|
|
* readfile ('https://www.w3c.org/'); |
|
58
|
|
|
* ?> |
|
59
|
|
|
* </code> |
|
60
|
|
|
*/ |
|
61
|
|
|
// public function __construct () |
|
62
|
|
|
public function __construct($keywords, $singlewords = false, $replace_callback = null) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->keywords = $keywords; |
|
65
|
|
|
$this->singlewords = $singlewords; |
|
66
|
|
|
$this->replace_callback = $replace_callback; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @access private |
|
71
|
|
|
* @param $replace_matches |
|
72
|
|
|
* @return mixed |
|
73
|
|
|
*/ |
|
74
|
|
|
public function replace($replace_matches) |
|
75
|
|
|
{ |
|
76
|
|
|
$patterns = []; |
|
77
|
|
|
if ($this->singlewords) { |
|
78
|
|
|
$keywords = \explode(' ', $this->preg_keywords); |
|
79
|
|
|
foreach ($keywords as $keyword) { |
|
80
|
|
|
$patterns[] = '/(?' . '>' . $keyword . '+)/si'; |
|
81
|
|
|
} |
|
82
|
|
|
} else { |
|
83
|
|
|
$patterns[] = '/(?' . '>' . $this->preg_keywords . '+)/si'; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$result = $replace_matches[0]; |
|
87
|
|
|
|
|
88
|
|
|
foreach ($patterns as $pattern) { |
|
89
|
|
|
if (null !== $this->replace_callback) { |
|
90
|
|
|
$result = \preg_replace_callback($pattern, [$this, 'replace_callback'], $result); |
|
91
|
|
|
} else { |
|
92
|
|
|
$result = \preg_replace($pattern, '<span class="highlightedkey">\\0</span>', $result); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $result; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @access private |
|
101
|
|
|
* @param $buffer |
|
102
|
|
|
* @return mixed|string |
|
103
|
|
|
*/ |
|
104
|
|
|
public function highlight($buffer) |
|
105
|
|
|
{ |
|
106
|
|
|
$buffer = '>' . $buffer . '<'; |
|
107
|
|
|
$this->preg_keywords = \preg_replace('/[^\w ]/si', '', $this->keywords); |
|
108
|
|
|
$buffer = \preg_replace_callback('/(\>(((?' . '>[^><]+)|(?R))*)\<)/is', [&$this, 'replace'], $buffer); |
|
109
|
|
|
$buffer = \xoops_substr($buffer, 1, -1); |
|
110
|
|
|
|
|
111
|
|
|
return $buffer; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|