本文整理汇总了PHP中DOMText::splitText方法的典型用法代码示例。如果您正苦于以下问题:PHP DOMText::splitText方法的具体用法?PHP DOMText::splitText怎么用?PHP DOMText::splitText使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DOMText
的用法示例。
在下文中一共展示了DOMText::splitText方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: _highlightTextNode
/**
* 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);
}
}
示例2: _highlightTextNode
/**
* 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);
}
}
示例3: _highlightTextNode
/**
* 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);
}
}
示例4: DOMComment
$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');