diff options
author | Andrew Dolgov <[email protected]> | 2013-08-01 16:30:22 +0400 |
---|---|---|
committer | Andrew Dolgov <[email protected]> | 2013-08-01 16:30:22 +0400 |
commit | 79834eda77cf2ce6f896269c242b357f6f1fe9d2 (patch) | |
tree | 7f3380145165f12fba4dde5555f9dca2c19f55d8 /include/functions.php | |
parent | 2554aa84cf04391a12a54aee5bf533bd2942b704 (diff) |
experimentally simplify highlight searching
Diffstat (limited to 'include/functions.php')
-rw-r--r-- | include/functions.php | 47 |
1 files changed, 20 insertions, 27 deletions
diff --git a/include/functions.php b/include/functions.php index ea31a40cc..b4789aec9 100644 --- a/include/functions.php +++ b/include/functions.php @@ -2864,35 +2864,28 @@ // 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); } } } |