The DOMCharacterData::substringData() function is an inbuilt function in PHP which is used to extracts a range of data from the node.
Syntax:
php
Output:
php
Output:
Reference: https://www.php.net/manual/en/domcharacterdata.substringdata.php
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.
<?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;
?>
GeeksForGeeksProgram 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();
?>
Reference: https://www.php.net/manual/en/domcharacterdata.substringdata.php