Instead of relying on unsafe regular expressions and string manipulation, we can utilize PHP's built-in DOM extension for modifying HTML.
The manipulate_html()
function from this snippet allows you to pass some HTML code, traverse & modify each of its DOM nodes in a callback and will return to you the modified HTML code.
The following example modifies all images in an HTML snippet to use lazy loading:
manipulate_html('<img src="foo.jpg">', function (DOMNode $node) {
if ($node->nodeName === 'img') {
$node->setAttribute('loading', 'lazy');
}
});
// Returns '<img src="foo.jpg" loading="lazy">'
This is just a single element, but you can test this snippet with any website:
manipulate_html(
file_get_contents('https://www.php.net/'),
function (DOMNode $node) {
// ...
}
);