PHP | DOMCharacterData replaceData() Function Last Updated : 20 Feb, 2020 Comments Improve Suggest changes Like Article Like Report The DOMCharacterData::replaceData() function is an inbuilt function in PHP which is used to replace a substring within the DOMCharacterData node. Syntax: void DOMCharacterData::replaceData( int $offset, int $count, string $data) Parameters: This function accept three parameters as mentioned above and described below: $offset: It specifies the starting position to delete the data. $count: It specifies the number of characters to delete. $data: It specifies the string with which the range must be replaced. Return Value: This function does not return any value. Exceptions: DOM_INDEX_SIZE_ERR is raised if $offset is negative or greater than the number of 16-bit units in data, or if $count is negative. Below given programs illustrate the DOMCharacterData::replaceData() function in PHP: Program 1 (Replacing Data from beginning): php <?php // Create a new DOM Document $dom = new DOMDocument('1.0', 'iso-8859-1'); // Create a div element $element = $dom->appendChild(new DOMElement('div')); // Create a DOMCdataSection $text = $element->appendChild( new DOMCdataSection('My DOM Characters')); // Replace the data $text->replaceData(0, 2, 'My Replaced'); echo $dom->saveXML(); ?> Output: <?xml version="1.0" encoding="iso-8859-1"?> <div><![CDATA[My Replaced DOM Characters]]></div> Use Chrome Developer tools to view the HTML or press Ctrl+U Program 2 (Replacing Data in the middle): php <?php // Create a new DOM Document $dom = new DOMDocument('1.0', 'iso-8859-1'); // Create a div element $element = $dom->appendChild(new DOMElement('div')); // Create a DOMCdataSection $text = $element->appendChild( new DOMCdataSection('GeeksErrorGeeks')); // Replace Data $text->replaceData(5, 5, 'For'); echo $dom->saveXML(); ?> Output: <?xml version="1.0" encoding="iso-8859-1"?> <div><![CDATA[GeeksForGeeks]]></div> Reference: https://www.php.net/manual/en/domcharacterdata.replacedata.php Comment More infoAdvertise with us Next Article PHP | DOMCharacterData replaceData() Function G gurrrung Follow Improve Article Tags : Web Technologies PHP PHP-function PHP-DOM Similar Reads PHP | DOMCharacterData substringData() Function The DOMCharacterData::substringData() function is an inbuilt function in PHP which is used to extracts a range of data from the node. Syntax: string DOMCharacterData::substringData( int $offset, int $count ) Parameters: This function accept two parameters as mentioned above and described below: $off 2 min read PHP | DOMCharacterData insertData() Function The DOMCharacterData::insertData() function is an inbuilt function in PHP which is used to insert a string at the specified 16-bit unit offset. Syntax: void DOMCharacterData::insertData( int $offset, string $data ) Parameters: This function accept two parameters as mentioned above and described belo 2 min read PHP array_replace() Function The array_replace() function is a builtin function in PHP and it takes a list of arrays separated by commas (,) as parameters and replaces all those values of the first array that have same keys in the other arrays. The replacement is done according to the following rules: If a key in the first arra 3 min read PHP | DOMCharacterData deleteData() Function The DOMCharacterData::deleteData() function is an inbuilt function in PHP which is used to remove a range of characters from the node. Syntax: void DOMCharacterData::deleteData( int $offset, int $count ) Parameters: This function accept two parameters as mentioned above and described below: $offset: 1 min read PHP | DOMCharacterData appendData() Function 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 tha 2 min read Like