DOMElement::insertAdjacentElement
(PHP 8 >= 8.3.0)
DOMElement::insertAdjacentElement — Insert adjacent element
Description
Inserts an element at a relative position given by where.
Parameters
where
beforebegin- Insert before the target element.afterbegin- Insert as the first child of the target element.beforeend- Insert as the last child of the target element.afterend- Insert after the target element.
element
The element to insert.
Return Values
Return DOMElement or null on failure.
Examples
Example #1 DOMElement::insertAdjacentElement() example
<?php
$dom = new DOMDocument();
$dom->loadXML('<?xml version="1.0"?><container><p>foo</p></container>');
$container = $dom->documentElement;
$p = $container->firstElementChild;
$p->insertAdjacentElement('beforebegin', $dom->createElement('A'));
echo $dom->saveXML();
?>The above example will output:
<?xml version="1.0"?> <container><A/><p>foo</p></container>
来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。
