experimentally simplify highlight searching
This commit is contained in:
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.'")' ;
|
|
||||||
}
|
|
||||||
$elements = $xpath->query('//*[contains(.,"'.$word.'")' . $also_query . ']');
|
|
||||||
|
|
||||||
foreach ($elements as $element) {
|
foreach ($elements as $child) {
|
||||||
foreach ($element->childNodes as $child) {
|
if (!$child instanceof DomText) continue;
|
||||||
|
|
||||||
if (!$child instanceof DomText) continue;
|
$fragment = $doc->createDocumentFragment();
|
||||||
|
$text = $child->textContent;
|
||||||
|
$stubs = array();
|
||||||
|
|
||||||
$fragment = $doc->createDocumentFragment();
|
while (($pos = mb_stripos($text, $word)) !== false) {
|
||||||
$text = $child->textContent;
|
$fragment->appendChild(new DomText(mb_substr($text, 0, $pos)));
|
||||||
$stubs = array();
|
$word = mb_substr($text, $pos, mb_strlen($word));
|
||||||
|
$highlight = $doc->createElement('span');
|
||||||
while (($pos = mb_stripos($text, $word)) !== false) {
|
$highlight->appendChild(new DomText($word));
|
||||||
$fragment->appendChild(new DomText(mb_substr($text, 0, $pos)));
|
$highlight->setAttribute('class', 'highlight');
|
||||||
$word = mb_substr($text, $pos, mb_strlen($word));
|
$fragment->appendChild($highlight);
|
||||||
$highlight = $doc->createElement('span');
|
$text = mb_substr($text, $pos + mb_strlen($word));
|
||||||
$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));
|
|
||||||
|
|
||||||
$element->replaceChild($fragment, $child);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!empty($text)) $fragment->appendChild(new DomText($text));
|
||||||
|
|
||||||
|
$child->parentNode->replaceChild($fragment, $child);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue