PHP | DOMText splitText() Function

The DOMText::splitText() function is an inbuilt function in PHP which is used to break a node into two nodes at the specified offset.

Syntax:

DOMText DOMText::splitText( int $offset )

Parameters: This function accepts a single parameter $offset which holds the offset to split.

Return Value: This function returns a new node of the same type, which contains all the content at and after the offset.

Below given programs illustrate the DOMText::splitText() function in PHP:



Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create the text Node
$text = new DOMText('GeeksforGeeks');
  
// Split the text
$splitedtext = $text->splitText(8);
echo $splitedtext->nodeValue;
?>

chevron_right


Output:

Geeks

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new DOMDocument instance
$document = new DOMDocument();
   
// Create a div element
$element = $document->appendChild(new DOMElement('div'));
   
// Create a text Node
$text = $document->createTextNode('GeeksforGeeks');
   
// Append the nodes
$element->appendChild($text);
   
echo "Number of text nodes before splitting: ";
echo count($element->childNodes) . "<br>";
   
// Splitting the text
$text->splitText(5);
   
echo "Number of text nodes after splitting: ";
echo count($element->childNodes);
?>

chevron_right


Output:

Number of text nodes before splitting: 1
Number of text nodes after splitting: 1

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