PHP 8.4.1 Released!

DOMElement::insertAdjacentElement

(PHP 8 >= 8.3.0)

DOMElement::insertAdjacentElementInsert adjacent element

Descrizione

public DOMElement::insertAdjacentElement(string $where, DOMElement $element): ?DOMElement

Inserts an element at a relative position given by where.

Elenco dei parametri

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.

Valori restituiti

Return DOMElement or null on failure.

Esempi

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();
?>

Il precedente esempio visualizzerà:

<?xml version="1.0"?>
<container><A/><p>foo</p></container>

Vedere anche:

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top