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 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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('GeeksGeeks'));
  
// Insert 'For' between Geeks and Geeks
$text->insertData(5, 'For');
  
echo $dom->saveXML();
?>

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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'));
  
// Insert in the beginning
$text->insertData(0, 'Beginning');
  
echo $dom->saveXML();
?>

chevron_right


Output:

<?xml version="1.0" encoding="iso-8859-1"?>
<div><![CDATA[BeginningMy DOM Characters]]></div>

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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'));
  
// Insert in the end
$text->insertData(17, 'End');
  
echo $dom->saveXML();
?>

chevron_right


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




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.