php8.5
Home/ Manual/ dom / domcharacterdata/ DOMCharacterData::replaceWith

DOMCharacterData::replaceWith

PHP function Edit on GitHub ✎

(PHP 8)

Replaces the character data with new nodes

Description

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

Replaces the character data with new nodes.

Examples

DOMCharacterData::replaceWith example

Replaces the character data with new nodes.

php
<?php
$doc = new DOMDocument;
$doc->loadXML("<container><![CDATA[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
  • DOMCharacterData::after
  • DOMCharacterData::before
  • DOMCharacterData::remove

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