panakour/phpdom

Some helpers around PHP DOM native classes

v0.0.1 2023-12-18 11:26 UTC

This package is auto-updated.

Last update: 2024-10-18 14:40:01 UTC


README

ci Code Coverage Badge

Panakour DOM

This is a PHP package that provides a simple and intuitive API for manipulating DOM elements.

Installation

You can install the package via composer:

composer require panakour/phpdom

Usage

First, you need to create a new instance of the Dom class:

$dom = new Dom('<html><body><p>Hello, World!</p></body></html>');

Getting a list of nodes

You can get a list of nodes with a specific tag name using the getNodeList method:

$images = $dom->getNodeList('img');

This will return a DOMNodeList containing all img elements in the document.

Getting attribute values

You can get the values of a specific attribute for all nodes with a specific tag name using the getAttributesValue method:

$imgSrc = $dom->getAttributesValue('img', 'src');

Replacing attributes

You can replace the attributes of all nodes with a specific tag name using the replaceAttributes method:

$dom->replaceAttributes('img', [
    ['src' => 'new-src.jpg', 'alt' => 'New image']
]);

This will replace the src and alt attributes of all img elements in the document.

Removing elements

You can remove all nodes with a specific tag name using the removeElementsAndItsContent method:

$dom->removeElementsAndItsContent(['img']);

This will remove all img elements from the document.

Explore More

Explore additional features and use cases in the unit tests found in /tests/DomTest.php.