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

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('GeeksForGeeks'));
  
// Get the substring
$text = $text->substringData(0, 13);
  
echo $text;
?>

chevron_right


Output:

GeeksForGeeks

Program 2 (Creating HTML heading to view substring):

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('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();
?>

chevron_right


Output:

Reference: https://www.php.net/manual/en/domcharacterdata.substringdata.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.