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:
- $offset: It specifies the starting position of substring to extract.
- $count: It specifies the number of characters to extract.
Return Value: This function returns the specified substring.
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::substringData() function in PHP:
Program 1 (Use PHP echo function to view substring):
<?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('GeeksForGeeks')); // Get the substring $text = $text->substringData(0, 13); echo $text; ?> |
Output:
GeeksForGeeks
Program 2 (Creating HTML heading to view substring):
<?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('GeeksForGeeks')); // Get the substring $text = $text->substringData(0, 13); // Create a new element $elementnew = $dom->createElement('h1', $text); // We insert the new element $dom->appendChild($elementnew); echo $dom->saveXML(); ?> |
Output:
Reference: https://www.php.net/manual/en/domcharacterdata.substringdata.php
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, zambiatek 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!




