CakeFest 2024: The Official CakePHP Conference

DOMText::splitText

(PHP 5, PHP 7, PHP 8)

DOMText::splitText 指定したオフセットでノードを 2 つに分割する

説明

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

指定したオフセット offset でノードを 2 つに分割します。分割したノードのツリー内での位置関係は、兄弟となります。

分割した後は、このノードは offset 位置までの 内容を保持するようになります。元のノードが親ノードを保持している場合、 新しいノードは元のノードの兄弟として元のノードの次の位置に挿入されます。 offset がこのノードの長さに等しい場合は、 新しいノードにはデータが含まれません。

パラメータ

offset

分割する位置を示すオフセット。0 から始まります。

戻り値

同じ型の新しいノードを返します。offset 以降の内容をデータとして保持します。

add a note

User Contributed Notes 1 note

up
1
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