
DOMDocumentFragment::replaceChildren는 자식 노드를 교체하는 데 사용되는 메서드입니다. 이 메서드는 모든 자식 노드를 삭제하고 새로운 노드를 삽입하는 방식으로 동작합니다.
이 메서드를 사용하여 모든 자식 노드를 삭제하고 새로운 노드를 삽입하려면, 다음과 같이 코드를 작성할 수 있습니다.
#hostingforum.kr
php
$doc = new DOMDocument();
$fragment = $doc->createDocumentFragment();
$fragment->appendChild($doc->createElement('div'));
$fragment->appendChild($doc->createElement('p'));
$doc->getElementsByTagName('body')->item(0)->appendChild($fragment);
$doc->getElementsByTagName('div')->item(0)->appendChild($doc->createElement('span'));
// 모든 자식 노드를 삭제하고 새로운 노드를 삽입
$fragment->replaceChildren($doc->createElement('div'));
// 새로운 노드의 자식 노드를 추가
$newNode = $fragment->firstChild;
$newNode->appendChild($doc->createElement('span'));
$newNode->appendChild($doc->createElement('p'));
이 코드에서는 replaceChildren 메서드를 사용하여 모든 자식 노드를 삭제하고 새로운 노드를 삽입합니다. 그리고 새로운 노드의 자식 노드를 추가하여 원하는 결과를 얻을 수 있습니다.
2025-05-16 10:41