+
+ if ($highlight_words) {
+ foreach ($highlight_words as $word) {
+
+ $elements = $xpath->query('//*[contains(.,"'.$word.'")]');
+
+ foreach ($elements as $element) {
+ foreach ($element->childNodes as $child) {
+
+ if (!$child instanceof DomText) continue;
+
+ $fragment = $doc->createDocumentFragment();
+ $text = $child->textContent;
+ $stubs = array();
+
+ while (($pos = stripos($text, $word)) !== false) {
+ $fragment->appendChild(new DomText(substr($text, 0, $pos)));
+ $word = substr($text, $pos, strlen($word));
+ $highlight = $doc->createElement('span');
+ $highlight->appendChild(new DomText($word));
+ $highlight->setAttribute('class', 'highlight');
+ $fragment->appendChild($highlight);
+ $text = substr($text, $pos + strlen($word));
+ }
+
+ if (!empty($text)) $fragment->appendChild(new DomText($text));
+
+ $element->replaceChild($fragment, $child);
+ }
+ }
+ }
+ }
+