panakour / phpdom
Some helpers around PHP DOM native classes
Requires
- php: >=8.1
- ext-dom: *
- ext-libxml: *
Requires (Dev)
- phpunit/phpunit: ^10.5.3
- vimeo/psalm: ^5.18.0
README
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.