1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
You may not change or alter any portion of this comment or credits |
4
|
|
|
of supporting developers from this source code or any supporting source code |
5
|
|
|
which is considered copyrighted (c) material of the original comment or credit authors. |
6
|
|
|
|
7
|
|
|
This program is distributed in the hope that it will be useful, |
8
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
9
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Xmf; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Highlighter |
16
|
|
|
* |
17
|
|
|
* @category Xmf\Highlighter |
18
|
|
|
* @package Xmf |
19
|
|
|
* @author Richard Griffith <[email protected]> |
20
|
|
|
* @copyright 2011-2016 XOOPS Project (http://xoops.org) |
21
|
|
|
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html) |
22
|
|
|
* @version Release: 1.0 |
23
|
|
|
* @link http://xoops.org |
24
|
|
|
* @since 1.0 |
25
|
|
|
*/ |
26
|
|
|
class Highlighter |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* mbstring encoding |
30
|
|
|
*/ |
31
|
|
|
const ENCODING = 'UTF-8'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Apply highlight to words in body text |
35
|
|
|
* |
36
|
|
|
* Surround occurrences of words in body with pre in front and post |
37
|
|
|
* behind. Considers only occurrences of words outside of HTML tags. |
38
|
|
|
* |
39
|
|
|
* @param string|string[] $words words to highlight |
40
|
|
|
* @param string $body body of html text to highlight |
41
|
|
|
* @param string $pre string to begin a highlight |
42
|
|
|
* @param string $post string to end a highlight |
43
|
|
|
* |
44
|
|
|
* @return string highlighted body |
45
|
|
|
*/ |
46
|
1 |
|
public static function apply($words, $body, $pre = '<strong>', $post = '</strong>') |
47
|
|
|
{ |
48
|
1 |
|
if (!is_array($words)) { |
49
|
1 |
|
$words = str_replace(' ', ' ', $words); |
50
|
1 |
|
$words = explode(' ', $words); |
51
|
|
|
} |
52
|
1 |
|
foreach ($words as $word) { |
53
|
1 |
|
$body = static::splitOnTag($word, $body, $pre, $post); |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
return $body; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* find needle in between html tags and add highlighting |
61
|
|
|
* |
62
|
|
|
* @param string $needle string to find |
63
|
|
|
* @param string $haystack html text to find needle in |
64
|
|
|
* @param string $pre insert before needle |
65
|
|
|
* @param string $post insert after needle |
66
|
|
|
* |
67
|
|
|
* @return mixed return from preg_replace_callback() |
68
|
|
|
*/ |
69
|
1 |
|
protected static function splitOnTag($needle, $haystack, $pre, $post) |
70
|
|
|
{ |
71
|
1 |
|
$encoding = static::ENCODING; |
72
|
1 |
|
return preg_replace_callback( |
73
|
1 |
|
'#((?:(?!<[/a-z]).)*)([^>]*>|$)#si', |
74
|
1 |
|
function ($capture) use ($needle, $pre, $post, $encoding) { |
75
|
1 |
|
$haystack = $capture[1]; |
76
|
1 |
|
if (function_exists('mb_substr')) { |
77
|
1 |
|
$p1 = mb_stripos($haystack, $needle, 0, $encoding); |
78
|
1 |
|
$l1 = mb_strlen($needle, $encoding); |
79
|
1 |
|
$ret = ''; |
80
|
1 |
|
while ($p1 !== false) { |
81
|
1 |
|
$ret .= mb_substr($haystack, 0, $p1, $encoding) . $pre |
82
|
1 |
|
. mb_substr($haystack, $p1, $l1, $encoding) . $post; |
83
|
1 |
|
$haystack = mb_substr($haystack, $p1 + $l1, null, $encoding); |
84
|
1 |
|
$p1 = mb_stripos($haystack, $needle, 0, $encoding); |
85
|
|
|
} |
86
|
|
|
} else { |
87
|
|
|
$p1 = stripos($haystack, $needle); |
88
|
|
|
$l1 = strlen($needle); |
89
|
|
|
$ret = ''; |
90
|
|
|
while ($p1 !== false) { |
91
|
|
|
$ret .= substr($haystack, 0, $p1) . $pre . substr($haystack, $p1, $l1) . $post; |
92
|
|
|
$haystack = substr($haystack, $p1 + $l1); |
93
|
|
|
$p1 = stripos($haystack, $needle); |
94
|
|
|
} |
95
|
|
|
} |
96
|
1 |
|
$ret .= $haystack . $capture[2]; |
97
|
|
|
|
98
|
1 |
|
return $ret; |
99
|
1 |
|
}, |
100
|
|
|
$haystack |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|