PHP | DOMNode normalize()函数
DOMNode::normalize()函数是PHP中的一个内置函数,用于删除空文本节点并合并该节点及其所有子节点中的相邻文本节点。
句法:
void DOMNode::normalize( void )
参数:此函数不接受任何参数。
返回值:此函数不返回任何值。
下面的例子说明了PHP中的DOMNode::normalize()函数:
示例 1:在这个程序中,我们将展示 normalize 如何删除空文本节点。
appendChild(new DOMElement('div'));
// Create a text Node
$text1 = $document->
createTextNode('GeeksforGeeks');
// Create a empty text Node
$text2 = $document->createTextNode('');
// Create another empty text Node
$text3 = $document->createTextNode('');
// Append the nodes
$element->appendChild($text1);
$element->appendChild($text2);
$element->appendChild($text3);
echo "Number of text nodes before normalization: ";
echo count($element->childNodes) . "
";
// Normalize the document
$document->normalize();
echo "Number of text nodes after normalization: ";
echo count($element->childNodes);
?>
输出:
Number of text nodes before normalization: 3
Number of text nodes after normalization: 1
示例 2:在这个程序中,我们将展示 normalize 如何合并所有相邻文本节点。
appendChild(new DOMElement('div'));
// Create a text Node
$text1 = $document->
createTextNode('Hello');
// Create another text Node
$text2 = $document->
createTextNode('World');
// Append the nodes
$element->appendChild($text1);
$element->appendChild($text2);
echo "Number of text nodes "
. "before normalization: ";
echo count($element->childNodes) . "
";
// Normalize the document
$document->normalize();
echo "Number of text nodes after "
. "normalization: ";
echo count($element->childNodes);
?>
输出:
Number of text nodes before normalization: 2
Number of text nodes after normalization: 1
参考: https://www. PHP.net/manual/en/domnode.normalize。 PHP