PHP | DOMCharacterData insertData() Function
Last Updated :
20 Feb, 2020
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 below:
- $offset: It specifies the starting point to insert data.
- $data: It specifies the data to insert.
Return Value: This function does not return any value.
Below given programs illustrate the DOMCharacterData::insertData() 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( 'GeeksGeeks' ));
$text ->insertData(5, 'For' );
echo $dom ->saveXML();
?>
|
Output:
<?xml version="1.0" encoding="iso-8859-1"?>
<div><![CDATA[GeeksForGeeks]]></div>
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( 'My DOM Characters' ));
$text ->insertData(0, 'Beginning' );
echo $dom ->saveXML();
?>
|
Output:
<?xml version="1.0" encoding="iso-8859-1"?>
<div><![CDATA[BeginningMy DOM Characters]]></div>
Program 3:
<?php
$dom = new DOMDocument( '1.0' , 'iso-8859-1' );
$element = $dom ->appendChild( new DOMElement( 'div' ));
$text = $element ->appendChild(
new DOMCdataSection( 'My DOM Characters' ));
$text ->insertData(17, 'End' );
echo $dom ->saveXML();
?>
|
Output:
<?xml version="1.0" encoding="iso-8859-1"?>
<div><![CDATA[My DOM CharactersEnd]]></div>
Reference: https://www.php.net/manual/en/domcharacterdata.insertdata.php
Share your thoughts in the comments
Please Login to comment...