/** * Highlight text in text node * * @param DOMText $node * @param array $wordsToHighlight * @param callback $callback Callback method, used to transform (highlighting) text. * @param array $params Array of additionall callback parameters (first non-optional parameter is a text to transform) * @throws \Zend\Search\Lucene\Exception\RuntimeException */ protected function _highlightTextNode(\DOMText $node, $wordsToHighlight, $callback, $params) { $analyzer = Analyzer\Analyzer::getDefault(); $analyzer->setInput($node->nodeValue, 'UTF-8'); $matchedTokens = array(); while (($token = $analyzer->nextToken()) !== null) { if (isset($wordsToHighlight[$token->getTermText()])) { $matchedTokens[] = $token; } } if (count($matchedTokens) == 0) { return; } $matchedTokens = array_reverse($matchedTokens); foreach ($matchedTokens as $token) { // Cut text after matched token $node->splitText($token->getEndOffset()); // Cut matched node $matchedWordNode = $node->splitText($token->getStartOffset()); // Retrieve HTML string representation for highlihted word $fullCallbackparamsList = $params; array_unshift($fullCallbackparamsList, $matchedWordNode->nodeValue); $highlightedWordNodeSetHTML = call_user_func_array($callback, $fullCallbackparamsList); // Transform HTML string to a DOM representation and automatically transform retrieved string // into valid XHTML (It's automatically done by loadHTML() method) $highlightedWordNodeSetDomDocument = new \DOMDocument('1.0', 'UTF-8'); $success = @$highlightedWordNodeSetDomDocument->loadHTML('<html><head><meta http-equiv="Content-type" content="text/html; charset=UTF-8"/></head><body>' . $highlightedWordNodeSetHTML . '</body></html>'); if (!$success) { throw new RuntimeException("Error occured while loading highlighted text fragment: '{$highlightedWordNodeSetHTML}'."); } $highlightedWordNodeSetXpath = new \DOMXPath($highlightedWordNodeSetDomDocument); $highlightedWordNodeSet = $highlightedWordNodeSetXpath->query('/html/body')->item(0)->childNodes; for ($count = 0; $count < $highlightedWordNodeSet->length; $count++) { $nodeToImport = $highlightedWordNodeSet->item($count); $node->parentNode->insertBefore($this->_doc->importNode($nodeToImport, true), $matchedWordNode); } $node->parentNode->removeChild($matchedWordNode); } }
/** * Highlight text in text node * * @param DOMText $node * @param array $wordsToHighlight * @param callback $callback Callback method, used to transform (highlighting) text. * @param array $params Array of additionall callback parameters (first non-optional parameter is a text to transform) * @throws Zend_Search_Lucene_Exception */ protected function _highlightTextNode(DOMText $node, $wordsToHighlight, $callback, $params) { /** Zend_Search_Lucene_Analysis_Analyzer */ require_once 'Zend/Search/Lucene/Analysis/Analyzer.php'; $analyzer = Zend_Search_Lucene_Analysis_Analyzer::getDefault(); $analyzer->setInput($node->nodeValue, 'UTF-8'); $matchedTokens = array(); while (($token = $analyzer->nextToken()) !== null) { if (isset($wordsToHighlight[$token->getTermText()])) { $matchedTokens[] = $token; } } if (count($matchedTokens) == 0) { return; } $matchedTokens = array_reverse($matchedTokens); foreach ($matchedTokens as $token) { // Cut text after matched token $node->splitText($token->getEndOffset()); // Cut matched node $matchedWordNode = $node->splitText($token->getStartOffset()); // Retrieve HTML string representation for highlihted word $fullCallbackparamsList = $params; array_unshift($fullCallbackparamsList, $matchedWordNode->nodeValue); $highlightedWordNodeSetHtml = call_user_func_array($callback, $fullCallbackparamsList); // Transform HTML string to a DOM representation and automatically transform retrieved string // into valid XHTML (It's automatically done by loadHTML() method) $highlightedWordNodeSetDomDocument = new DOMDocument('1.0', 'UTF-8'); $success = @$highlightedWordNodeSetDomDocument->loadHTML($highlightedWordNodeSetHtml); if (!$success) { require_once 'Zend/Search/Lucene/Exception.php'; throw new Zend_Search_Lucene_Exception("Error occured while loading highlighted text fragment: '{$highlightedWordNodeSetHtml}'."); } $highlightedWordNodeSetXpath = new DOMXPath($highlightedWordNodeSetDomDocument); $highlightedWordNodeSet = $highlightedWordNodeSetXpath->query('/html/body')->item(0)->childNodes; for ($count = 0; $count < $highlightedWordNodeSet->length; $count++) { $nodeToImport = $highlightedWordNodeSet->item($count); $node->parentNode->insertBefore($this->_doc->importNode($nodeToImport, true), $matchedWordNode); } $node->parentNode->removeChild($matchedWordNode); } }
/** * Highlight text in text node * * @param DOMText $node * @param array $wordsToHighlight * @param string $color */ public function _highlightTextNode(DOMText $node, $wordsToHighlight, $color) { $analyzer = Zend_Search_Lucene_Analysis_Analyzer::getDefault(); $analyzer->setInput($node->nodeValue, $this->_doc->encoding); $matchedTokens = array(); while (($token = $analyzer->nextToken()) !== null) { if (isset($wordsToHighlight[$token->getTermText()])) { $matchedTokens[] = $token; } } if (count($matchedTokens) == 0) { return; } $matchedTokens = array_reverse($matchedTokens); foreach ($matchedTokens as $token) { // Cut text after matched token $node->splitText($token->getEndOffset()); // Cut matched node $matchedWordNode = $node->splitText($token->getStartOffset()); $highlightedNode = $this->_doc->createElement('b', $matchedWordNode->nodeValue); $highlightedNode->setAttribute('style', 'color:black;background-color:' . $color); $node->parentNode->replaceChild($highlightedNode, $matchedWordNode); } }
$comment = new DOMComment('Testing character data and extending nodes'); $charnode->appendChild($comment); echo "Comment Length: " . $comment->length . "\n"; $comment->data = 'Updated comment'; echo "New Comment Length: " . $comment->length . "\n"; echo "New Comment Data: " . $comment->data . "\n"; /* DOMCDataSection */ $cdata = new DOMCDataSection('Chars: <>&"'); $charnode->appendChild($cdata); echo "Substring: " . $cdata->substringData(7, 4) . "\n"; $cdata->replaceData(10, 1, "'"); echo "New Substring: " . $cdata->substringData(7, 4) . "\n"; /* DOMCharacterData using DOMComment */ $comment = new DOMComment('instructions'); echo "Comment Value: " . $comment->data . "\n"; $comment->data = 'some more instructions'; echo "New Comment Value: " . $comment->data . "\n"; $comment->insertData(10, 'pi '); $comment->replaceData(18, 5, 'i'); $comment->insertData(20, 'g'); $comment->deleteData(13, 2); $comment->deleteData(10, 3); $comment->insertData(10, 'comment '); echo "Updated Comment Value: " . $comment->data . "\n"; /* DOMText */ $text = new DOMText('some text characters'); echo "Whole Text: " . $text->wholeText . "\n"; $text2 = $text->splitText(9); echo "Split text: " . $text2->wholeText . "\n"; $text3 = $text2->splitText(1); echo "Is Whitespace?: " . ($text2->isElementContentWhitespace() ? 'YES' : 'NO');