DOMText::splitText

(PHP 5, PHP 7, PHP 8)

DOMText::splitText Breaks this node into two nodes at the specified offset

Description

public DOMText::splitText(int $offset): DOMText|false

Breaks this node into two nodes at the specified offset, keeping both in the tree as siblings.

After being split, this node will contain all the content up to the offset. If the original node had a parent node, the new node is inserted as the next sibling of the original node. When the offset is equal to the length of this node, the new node has no data.

Parameters

offset

The offset at which to split, starting from 0.

Return Values

The new node of the same type, which contains all the content at and after the offset.

add a note add a note

User Contributed Notes 1 note

up
5
Flix Cloutier
10 years ago
It should be noted that $offset is a **character offset**, not a **byte offset**. This means that most other PHP string functions that deal with lengths and offsets (strlen, strpos, preg_match with PREG_OFFSET_CAPTURE, etc.) use and return values unsuitable for this method if used with multibyte strings (like UTF-8 strings).

Byte offsets can be converted to character offsets with mb_strlen:

<?php
function char_offset($string, $byte_offset, $encoding = null)
{
   
$substr = substr($string, 0, $byte_offset);
    return
mb_strlen($substr, $encoding ?: mb_internal_encoding());
}
?>
To Top