public highlightExtended ( string | array $words, callback $callback, array $params = [] ) : string | ||
$words | string | array | Words to highlight. Words could be organized using the array or string. |
$callback | callback | Callback method, used to transform (highlighting) text. |
$params | array | Array of additionall callback parameters passed through into it (first non-optional parameter is an HTML fragment for highlighting) |
return | string |
public function highlightExtended($words, $callback, $params = [])
{
/** Zend_Search_Lucene_Analysis_Analyzer */
require_once 'Zend/Search/Lucene/Analysis/Analyzer.php';
if (!is_array($words)) {
$words = [$words];
}
$wordsToHighlightList = [[]];
$analyzer = Zend_Search_Lucene_Analysis_Analyzer::getDefault();
foreach ($words as $wordString) {
$wordsToHighlightList[] = $analyzer->tokenize($wordString, 'UTF-8');
}
$wordsToHighlight = call_user_func_array('array_merge', $wordsToHighlightList);
if (count($wordsToHighlight) == 0) {
return $this->_doc->saveHTML();
}
$wordsToHighlightFlipped = [];
foreach ($wordsToHighlight as $id => $token) {
$wordsToHighlightFlipped[$token->getTermText()] = $id;
}
if (!is_callable($callback)) {
require_once 'Zend/Search/Lucene/Exception.php';
throw new Zend_Search_Lucene_Exception('$viewHelper parameter must be a View Helper name, View Helper object or callback.');
}
$xpath = new DOMXPath($this->_doc);
$matchedNodes = $xpath->query("/html/body");
foreach ($matchedNodes as $matchedNode) {
$this->_highlightNodeRecursive($matchedNode, $wordsToHighlightFlipped, $callback, $params);
}
}