php8.5
Home/ Manual/ dom / domelement/ DOMElement::replaceChildren

DOMElement::replaceChildren

PHP function Edit on GitHub ✎

(PHP 8 >= 8.3.0)

Replace children in element

Description

DOMElement::replaceChildren(DOMNode|string ...$nodes): void

Replaces the children in the element with new nodes.

Examples

DOMElement::replaceChildren example

Replaces the children with new nodes.

php
<?php
$doc = new DOMDocument;
$doc->loadXML("<container><hello/></container>");
$container = $doc->documentElement;

$container->replaceWith("beautiful", $doc->createElement("world"));

echo $doc->saveXML();
?>

The above example will output:

output
<?xml version="1.0"?>
beautiful
<world/>

See Also

  • DOMParentNode::replaceChildren
  • DOMElement::replaceWith
  • DOMElement::after
  • DOMElement::before
  • DOMElement::remove

Source: reference/dom/domelement/replacechildren.xml · from the official PHP manual (php/doc-en)