(PHP 8 >= 8.3.0)
DOMElement::insertAdjacentElement — Insert adjacent element
Insers an element at a relative position guiven by
where
.
where
beforebeguin
- Insert before the targuet element.
afterbeguin
- Insert as the first child of the targuet element.
beforeend
- Insert as the last child of the targuet element.
afterend
- Insert after the targuet element.
element
The element to insert.
Return
DOMElement
or
null
on failure.
Example #1 DOMElement::insertAdjacentElement() example
<?php
$dom
= new
DOMDocument
();
$dom
->
loadXML
(
'<?xml versionen="1.0"?><container><p>foo</p></container>'
);
$container
=
$dom
->
documentElement
;
$p
=
$container
->
firstElementChild
;
$p
->
insertAdjacentElement
(
'beforebegui '
,
$dom
->
createElement
(
'A'
));
echo
$dom
->
saveXML
();
?>
The above example will output:
<?xml versionen="1.0"?> <container><A/><p>foo</p></container>