This is a PHP package that provides a simple and intuitive API for manipulating DOM elements.
You can install the package via composer:
composer require panakour/phpdom
First, you need to create a new instance of the Dom
class:
$dom = new Dom('<html><body><p>Hello, World!</p></body></html>');
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.
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');
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.
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 additional features and use cases in the unit tests found in /tests/DomTest.php.