]> git.wh0rd.org Git - tt-rss.git/commitdiff
experimentally simplify highlight searching
authorAndrew Dolgov <fox@madoka.volgo-balt.ru>
Thu, 1 Aug 2013 12:30:22 +0000 (16:30 +0400)
committerAndrew Dolgov <fox@madoka.volgo-balt.ru>
Thu, 1 Aug 2013 12:30:22 +0000 (16:30 +0400)
include/functions.php

index ea31a40cc06899c6d36ca408a810f1d66e8e9ee4..b4789aec99e251e8e32bb4b9764d184119ef919d 100644 (file)
 
                                // http://stackoverflow.com/questions/4081372/highlight-keywords-in-a-paragraph
 
-                               $also_query = '';
-                               foreach(array(strtolower($word), strtoupper($word), ucfirst(strtolower($word))) as $word_cap) {
-                                       $also_query .= ' or contains(.,"'.$word_cap.'")' ;
-                               }
-                               $elements = $xpath->query('//*[contains(.,"'.$word.'")' . $also_query . ']');
-
-                               foreach ($elements as $element) {
-                                       foreach ($element->childNodes as $child) {
-
-                                               if (!$child instanceof DomText) continue;
-
-                                               $fragment = $doc->createDocumentFragment();
-                                               $text = $child->textContent;
-                                               $stubs = array();
-
-                                               while (($pos = mb_stripos($text, $word)) !== false) {
-                                                       $fragment->appendChild(new DomText(mb_substr($text, 0, $pos)));
-                                                       $word = mb_substr($text, $pos, mb_strlen($word));
-                                                       $highlight = $doc->createElement('span');
-                                                       $highlight->appendChild(new DomText($word));
-                                                       $highlight->setAttribute('class', 'highlight');
-                                                       $fragment->appendChild($highlight);
-                                                       $text = mb_substr($text, $pos + mb_strlen($word));
-                                               }
+                               $elements = $xpath->query("//*/text()");
+
+                               foreach ($elements as $child) {
+                                       if (!$child instanceof DomText) continue;
+
+                                       $fragment = $doc->createDocumentFragment();
+                                       $text = $child->textContent;
+                                       $stubs = array();
+
+                                       while (($pos = mb_stripos($text, $word)) !== false) {
+                                               $fragment->appendChild(new DomText(mb_substr($text, 0, $pos)));
+                                               $word = mb_substr($text, $pos, mb_strlen($word));
+                                               $highlight = $doc->createElement('span');
+                                               $highlight->appendChild(new DomText($word));
+                                               $highlight->setAttribute('class', 'highlight');
+                                               $fragment->appendChild($highlight);
+                                               $text = mb_substr($text, $pos + mb_strlen($word));
+                                       }
 
-                                               if (!empty($text)) $fragment->appendChild(new DomText($text));
+                                       if (!empty($text)) $fragment->appendChild(new DomText($text));
 
-                                               $element->replaceChild($fragment, $child);
-                                       }
+                                       $child->parentNode->replaceChild($fragment, $child);
                                }
                        }
                }