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

DOMElement::replaceWith

PHP function Edit on GitHub ✎

(PHP 8)

Replaces the element with new nodes

Description

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

Replaces the element with new nodes.

Examples

DOMElement::replaceWith example

Replaces the element with new nodes.

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

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

echo $doc->saveXML();
?>

The above example will output:

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

See Also

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

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