PHP | DOMCharacterData appendData() Function
Last Updated :
20 Feb, 2020
The DOMCharacterData::appendData() function is an inbuilt function in PHP which is used to append the string to the end of the character data of the node.
Syntax:
public DOMCharacterData::appendData( string $data )
Parameters: This function accepts a single parameter $data which holds the string that need to append.
Return Value: This function does not return any value.
Below given programs illustrate the DOMCharacterData::appendData() function in PHP:
Program 1:
<?php
$dom = new DOMDocument( '1.0' , 'iso-8859-1' );
$element = $dom ->appendChild( new DOMElement( 'div' ));
$text = $element ->appendChild(
new DOMCdataSection( 'Hey this is my DOMC' ));
$text ->appendData( ' This is appended text' );
echo $dom ->saveXML();
?>
|
<?xml version="1.0" encoding="iso-8859-1"?>
<div>
<![CDATA[Hey this is my DOMC This is appended text]]>
</div>
Output: Use Chrome Developer tools to view the HTML or press Ctrl+U
Program 2:
<?php
$dom = new DOMDocument( '1.0' , 'iso-8859-1' );
$element = $dom ->appendChild( new DOMElement( 'div' ));
$text = $element ->appendChild(
new DOMCdataSection( 'DOMC Data' ));
$text ->deleteData(0, 9);
$text ->appendData( ' This is appended text' );
echo $dom ->saveXML();
?>
|
Output:
<?xml version="1.0" encoding="iso-8859-1"?>
<div><![CDATA[ This is appended text]]></div>
Reference: https://www.php.net/manual/en/domcharacterdata.appenddata.php
Share your thoughts in the comments
Please Login to comment...