PHP | DOMCharacterData substringData() Function

Last Updated : 20 Feb, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
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
<?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
<?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

Next Article

Similar Reads