Friday, October 10, 2025
HomeLanguagesPHP | DOMText splitText() Function

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:




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


Output:

Geeks

Program 2:




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


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

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32350 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6718 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7100 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS