PHP | DOMNode normalize() Function
The DOMNode::normalize() function is an inbuilt function in PHP which is used to remove empty text nodes and merge adjacent text nodes in this node and all its children.
Syntax:
void DOMNode::normalize( void )
Parameters: This function doesn’t accept any parameters.
Return Value: This function does not return any value.
Below examples illustrate the DOMNode::normalize() function in PHP:
Example 1: In this program, we will show how normalize removes empty text nodes.
<?php
$document = new DOMDocument();
$element = $document ->
appendChild( new DOMElement( 'div' ));
$text1 = $document ->
createTextNode( 'GeeksforGeeks' );
$text2 = $document ->createTextNode( '' );
$text3 = $document ->createTextNode( '' );
$element ->appendChild( $text1 );
$element ->appendChild( $text2 );
$element ->appendChild( $text3 );
echo "Number of text nodes before normalization: " ;
echo count ( $element ->childNodes) . "<br>" ;
$document ->normalize();
echo "Number of text nodes after normalization: " ;
echo count ( $element ->childNodes);
?>
|
Output:
Number of text nodes before normalization: 3
Number of text nodes after normalization: 1
Example 2: In this program, we will show how normalize merges all neighbor text nodes.
<?php
$document = new DOMDocument();
$element = $document ->
appendChild( new DOMElement( 'div' ));
$text1 = $document ->
createTextNode( 'Hello' );
$text2 = $document ->
createTextNode( 'World' );
$element ->appendChild( $text1 );
$element ->appendChild( $text2 );
echo "Number of text nodes "
. "before normalization: " ;
echo count ( $element ->childNodes) . "<br>" ;
$document ->normalize();
echo "Number of text nodes after "
. "normalization: " ;
echo count ( $element ->childNodes);
?>
|
Output:
Number of text nodes before normalization: 2
Number of text nodes after normalization: 1
Reference: https://www.php.net/manual/en/domnode.normalize.php
Last Updated :
02 Mar, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...