php8.5
Home/ Manual/ dom / domparentnode/ DOMParentNode::replaceChildren

DOMParentNode::replaceChildren

PHP function Edit on GitHub ✎

(PHP 8 >= 8.3.0)

Replace children in node

Description

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

Replace children in node.

Parameters

nodes

The nodes replacing the children. Strings are automatically converted to text nodes.

Return Values

Void

Errors/Exceptions

Changelog

VersionDescription
8.3.0Calling this method on a node without an owner document now works. Previous_hierarchy_exception

Examples

DOMParentNode::replaceChildren example

php
<?php

$dom = new DOMDocument();
$dom->loadHTML('<html><p>hi</p> test <p>hi2</p></html>');

$dom->documentElement->replaceChildren('foo', $dom->createElement('p'), 'bar');
echo $dom->saveXML();
?>

The above example will output:

output
<?xml version="1.0" standalone="yes"?>
<!DOCTYPE HTML>
<html>foo<p/>bar</html>

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