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
nodesThe nodes replacing the children. Strings are automatically converted to text nodes.
Return Values
Void
Errors/Exceptions
Changelog
| Version | Description |
|---|---|
| 8.3.0 | Calling 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>