experimentally simplify highlight searching

master
Andrew Dolgov 11 years ago
parent 2554aa84cf
commit 79834eda77

@ -2864,35 +2864,28 @@
// http://stackoverflow.com/questions/4081372/highlight-keywords-in-a-paragraph // http://stackoverflow.com/questions/4081372/highlight-keywords-in-a-paragraph
$also_query = ''; $elements = $xpath->query("//*/text()");
foreach(array(strtolower($word), strtoupper($word), ucfirst(strtolower($word))) as $word_cap) {
$also_query .= ' or contains(.,"'.$word_cap.'")' ; foreach ($elements as $child) {
} if (!$child instanceof DomText) continue;
$elements = $xpath->query('//*[contains(.,"'.$word.'")' . $also_query . ']');
$fragment = $doc->createDocumentFragment();
foreach ($elements as $element) { $text = $child->textContent;
foreach ($element->childNodes as $child) { $stubs = array();
if (!$child instanceof DomText) continue; while (($pos = mb_stripos($text, $word)) !== false) {
$fragment->appendChild(new DomText(mb_substr($text, 0, $pos)));
$fragment = $doc->createDocumentFragment(); $word = mb_substr($text, $pos, mb_strlen($word));
$text = $child->textContent; $highlight = $doc->createElement('span');
$stubs = array(); $highlight->appendChild(new DomText($word));
$highlight->setAttribute('class', 'highlight');
while (($pos = mb_stripos($text, $word)) !== false) { $fragment->appendChild($highlight);
$fragment->appendChild(new DomText(mb_substr($text, 0, $pos))); $text = mb_substr($text, $pos + mb_strlen($word));
$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);
}
} }
} }
} }

Loading…
Cancel
Save