
DomText::splitText 메서드는 textNode의 특정 위치에서 textNode를 분할하여 새로운 textNode를 반환합니다. 이 메서드를 사용할 때, 새로운 textNode의 nodeValue가 null이 되는 것은 textNode의 자식 노드가 없을 때 발생하는 현상입니다.
이러한 현상은 textNode가 단일 텍스트 노드일 때 발생합니다. textNode가 단일 텍스트 노드인 경우, splitText 메서드는 새로운 textNode를 반환하지만, nodeValue는 null로 초기화됩니다.
위 코드에서, 'Hello World'를 5번째 위치에서 분할하였습니다. 이 경우, 'Hello'와 ' World'가 각각 textNode로 생성됩니다. 'Hello' textNode의 nodeValue는 'Hello'로 초기화되며, ' World' textNode의 nodeValue는 null로 초기화됩니다.
이러한 현상은 해결할 수 없습니다. 그러나, textNode의 자식 노드를 추가하여 nodeValue를 초기화할 수 있습니다. 예를 들어, ' World' textNode의 nodeValue를 ' World'로 초기화할 수 있습니다.
#hostingforum.kr
php
$textNode = $document->createTextNode('Hello World');
$splitTextNode = $textNode->splitText(5);
$splitTextNode->nodeValue = ' World';
또는, textNode를 생성할 때 nodeValue를 초기화할 수 있습니다.
#hostingforum.kr
php
$textNode = $document->createTextNode('Hello World');
$textNode->nodeValue = 'Hello World';
$splitTextNode = $textNode->splitText(5);
2025-05-29 01:40