Saturday, September 28, 2024
Google search engine
HomeLanguagesPHP | DOMCharacterData deleteData() Function

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: It specifies the starting position to deleted data.
  • $count: It specifies the number of characters to delete.

Return Value: This function does not return any value.

Below given programs illustrate the DOMCharacterData::deleteData() function in PHP:

Program 1:




<?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('DOMC Data'));
  
// Delete all data
$text->deleteData(0, 9);
  
echo $dom->saveXML();
?>


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

Output: Use Chrome Developer tools to view the HTML or press Ctrl+U

Program 2:




<?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('DOMC Data'));
  
// Delete all data
$text->deleteData(0, 9);
  
// Adding new data again
$text->insertData(0, 'New data');
  
echo $dom->saveXML();
?>


<?xml version="1.0" encoding="iso-8859-1"?>
<div><![CDATA[New data]]></div>

Output:

Reference: https://www.php.net/manual/en/domcharacterdata.deletedata.php

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments