LEPTON CMS 7.2.0
feel free to keep it strictly simple...
Loading...
Searching...
No Matches
function.search_highlight.php
Go to the documentation of this file.
1<?php
2
18// include secure.php to protect this file and the whole CMS!
19if(!defined("SEC_FILE")){define("SEC_FILE",'/framework/secure.php' );}
20if (defined('LEPTON_PATH')) {
21 include LEPTON_PATH.SEC_FILE;
22} else {
23 $oneback = "../";
24 $root = $oneback;
25 $level = 1;
26 while (($level < 10) && (!file_exists($root.SEC_FILE))) {
27 $root .= $oneback;
28 $level += 1;
29 }
30 if (file_exists($root.SEC_FILE)) {
31 include $root.SEC_FILE;
32 } else {
33 trigger_error(sprintf("[ <b>%s</b> ] Can't include secure.php!", $_SERVER['SCRIPT_NAME']), E_USER_ERROR);
34 }
35}
36// end include secure file
37
38
48function search_highlight(string $sContent='', array $aTermsToMark=array()): string
49{
50 if ($sContent === "")
51 {
52 return "";
53 }
54
55 array_walk($aTermsToMark,
56 function( &$v )
57 {
58 $v = preg_quote($v, '~');
59 }
60 );
61 $search_string = implode("|", $aTermsToMark);
62 $string = $search_string;//str_replace($string_ul_umlaut, $string_ul_regex, $search_string);
63 /*
64 the highlighting
65 match $string, but not inside <style>...</style>, <script>...</script>, <!--...--> or HTML-Tags
66 Also droplet tags are now excluded from highlighting.
67 split $string into pieces - "cut away" styles, scripts, comments, HTML-tags and eMail-addresses
68 we have to cut <pre> and <code> as well.
69 for HTML-Tags use <(?:[^<]|<.*>)*> which will match strings like <input ... value="<b>value</b>" >
70 */
71 $matches = preg_split("~(\[\[.*\]\]|<style.*</style>|<script.*</script>|<pre.*</pre>|<code.*</code>|<!--.*-->|<(?:[^<]|<.*>)*>|\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,8}\b)~iUs",$sContent,-1,(PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY));
72 if (is_array($matches) && $matches != array())
73 {
74 $sContent = "";
75 foreach ($matches as $match)
76 {
77 if ($match[0] != "<" && !preg_match('/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,8}$/i', $match) && !preg_match('~\[\[.*\]\]~', $match))
78 {
79 $match = str_replace(
80 array('&lt;', '&gt;', '&amp;', '&quot;', '&#039;', '&nbsp;'),
81 array('<', '>', '&', '"', '\'', "\xC2\xA0"),
82 $match
83 );
84
85 $match = preg_replace(
86 '~('.$string.')~ui',
87 '_span class=_highlight__$1_/span_',
88 $match
89 );
90
91 $match = str_replace(
92 array('&', '<', '>', '"', '\'', "\xC2\xA0"),
93 array('&amp;', '&lt;', '&gt;', '&quot;', '&#039;', '&nbsp;'),
94 $match
95 );
96
97 $match = str_replace(
98 array('_span class=_highlight__', '_/span_'),
99 array('<span class="highlight">', '</span>'),
100 $match
101 );
102 }
103 $sContent .= $match;
104 }
105 }
106
107 return $sContent;
108}
search_highlight(string $sContent='', array $aTermsToMark=array())