Working with XML (HTML) documents is a necessary task for many web applications and the dom extension implements a standardized API that was previously specified by a w3 group into 3 DOM Levels. Since then the standard has evolved and is now a Living Standard similar to HTML 5 and continuously evolving and maintained by the Web Hypertext Application Technology Working Group (WHATWG).

A new RFC was created to improve the DOM API based on changes in the DOM Standard in the last few years, and was accepted for PHP 8.0 and will add a bunch of interesting functionalities. This RFC will improve a lot of common use cases manipulating and traversing DOM documents. This shows firstElementChild traversal and the after() manipulation:

<?php

$dom = new DOMDocument;
$dom->loadXML('<root><mark>first</mark></root>');

$element = $dom->documentElement->firstElementChild;

$element->after(
    'text inserted after',
    $dom->createElement('mark', 'second')
);

echo $dom->saveXML();
/*
<?xml version="1.0"?>
<root><mark>first</mark>text inserted after<mark>second</mark></root>
*/

More information about the DOM living standard API RFC at https://wiki.php.net/rfc/dom_living_standard_api

LEAVE A REPLY

Please enter your comment!
Please enter your name here