PHPackages                             cse/dom-manager - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. cse/dom-manager

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

cse/dom-manager
===============

A simple library for management the DOM (XML, HTML) document.

301PHP

Since Jan 15Pushed 4y ago2 watchersCompare

[ Source](https://github.com/cs-eliseev/dom-manager)[ Packagist](https://packagist.org/packages/cse/dom-manager)[ RSS](/packages/cse-dom-manager/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

English | [Русский](https://github.com/cs-eliseev/dom-manager/blob/master/README.ru_RU.md)

DOM Manager
===========

[](#dom-manager)

A simple library for management the DOM (XML, HTML) document.

Project repository:

**DEMO**

```
use cse\DOMManager\DomManager;
...
$content = file_get_contents('https://www.w3schools.com/xml/simple.xml');
$easyLoader = new DomManager();
$node = $easyLoader->parse($content);
$name = $node->find('name')->first()->text(); // Belgian Waffles
```

---

Install
-------

[](#install)

You can find the most recent version of this project [here](https://github.com/cs-eliseev/dom-manager).

### Composer

[](#composer)

Execute the following command to get the latest version of the package:

```
composer require cse/dom-manager
```

Or file composer.json should include the following contents:

```
{
    "require": {
        "cse/dom-manager": "*"
    }
}
```

### Git

[](#git)

Clone this repository locally:

```
git clone https://github.com/cs-eliseev/dom-manager.git
```

### Download

[](#download)

[Download the latest release here](https://github.com/cs-eliseev/dom-manager/archive/master.zip).

Usage
-----

[](#usage)

The following demonstrates how to use the functions of this library in a PHP application.

### Init

[](#init)

The DomManager instance convert data into a structure for manipulating the DOM tree.

Converting XML structure:

```
use cse\DOMManager\IDomManager;
...
$easyLoader = new IDomManager();
$nodes = $easyLoader->parse(''); // Nodes entity
```

Converting HTML structure via DOMDocument:

```
use cse\DOMManager\DomManager;
...
$domDocument = new DOMDocument('1.0', 'UTF-8');
$domDocument->loadHTML('');
$easyLoader = new DomManager();
$nodes = $easyLoader->parse($domDocument); // Nodes entity
```

Converting DOMNodeList:

```
use cse\DOMManager\DomManager;
...
$domDocument = new DOMDocument('1.0', 'UTF-8');
$domDocument->loadXML('');

$easyLoader = new DomManager();
$nodes = $easyLoader->parse($domDocument->getElementsByTagName('br')); // Nodes entity
```

Converting DOMNode:

```
use cse\DOMManager\DomManager;
...
$domDocument = new DOMDocument('1.0', 'UTF-8');
$domDocument->loadXML('');

$easyLoader = new DomManager();
$nodes = $easyLoader->parse($domDocument->getElementsByTagName('br')->item(1)); // Nodes entity
```

An instance of the `Nodes` represents a list of DOMNode objects, can be manipulation.

### Methods

[](#methods)

\#MethodParametersReturn valuesDescriptionNavigation methods[\#](#find)find($node\_name)$node\_name - search parameterNodesReturns all found node elements[\#](#first)first($node\_name)$node\_name - search parameter (optional)NodesReturns the first found node element[\#](#last)last($node\_name)$node\_name - search parameter (optional)NodesReturns the last found node element[\#](#getByPosition)getByPosition($position, $node\_name)$position - number index, $node\_name - search parameter (optional)NodesReturns the element by index[\#](#closest)closest($node\_name)$node\_name - parent element search parameter (optional)NodesReturns the closest parent element[\#](#parent)parent()NodesReturns the parent element[\#](#root)root()NodesReturns the root element[\#](#childes)childes($node\_name)$node\_name - search parameter (optional)NodesReturns all children of the node[\#](#firstChild)firstChild($node\_name)$node\_name - search parameter (optional)NodesReturns the first child of the node[\#](#getNodeByPosition)getNodeByPosition($position)$position - number indexDOMNodeReturns the DOMNode element by index[\#](#findNodeByPosition)findNodeByPosition($position, $node\_name)$position - number index, $node\_name - search parameterNodesReturns the found DOMNode element by indexNode management methods[\#](#name)name()stringReturns node name[\#](#rename)rename($new\_node\_name, $old\_node\_name)$new\_node\_name - new node name, $old\_node\_name - search parameter (optional)NodesRename node[\#](#replace)replace($content, $node\_name)$content - replacement data, $node\_name - search parameter (optional)NodesReplace node[\#](#remove)remove($node\_name)$node\_name - search parameter (optional)NodesRemove nodeNode information[\#](#count)count($node\_name)$node\_name - search parameter (optional)intCounting the number of elements[\#](#exist)exist($node\_name)$node\_name - search parameter (optional)boolChecking for node existence[\#](#notExist)notExist($node\_name)$node\_name - search parameter (optional)boolChecking for node not existence[\#](#isElem)isElem()boolChecking that the number of elements is 1[\#](#isList)isList()boolChecks that the number of elements is more than 1[\#](#isDomComment)isDomComment()boolValidation DOMComment[\#](#isDomElement)isDomElement()boolValidation DOMElement[\#](#isDomText)isDomText()boolValidation DOMText[\#](#type)type()stringReturn typeMethods for management text[\#](#text)text()stringReturns text[\#](#addText)addText($text)$text - adding textNodesAdds text to the end of a node[\#](#replaceText)replaceText($text)$text - replace textNodesReplacing text in a node[\#](#removeText)removeText()NodesRemove text in a nodeMethods for management attributes[\#](#attr)attr($attribute, $default)$attribute - attribute name, $default - default value (optional)stringReturn attribute value[\#](#hasAttr)hasAttr($attribute)$attribute - attribute nameboolChecking if an attribute exists[\#](#setAttr)setAttr($attribute, $value)$attribute - attribute name, $value - value to insertNodesSet attribute value[\#](#removeAttr)removeAttr($attribute)$attribute - attribute nameNodesRemove attributeMethods for management childes[\#](#appendChild)appendChild($content, $node\_name)$content - replace data, $node\_name - search parameter (optional)NodesAdding child node[\#](#replaceChildes)replaceChildes($content, $node\_name)$content - replacement data, $node\_name - search parameter (optional)NodesReplace childes node[\#](#removeChildes)removeChildes($node\_name)$node\_name - search parameter (optional)NodesRemove childes nodeTransform methods[\#](#toArray)toArray()DOMNode\[\]Returns a list of DOMNode elements[\#](#toList)toList()NodeListReturn NodeList[\#](#toString)toString($node\_name)$node\_name - search parameter (optional)stringReturns the markup of an element, including its content.List iteration methods[\#](#each)each(function ($elem) {})Calls a callback function for each item### Example

[](#example)

Example of document content:

```

            Header-subheader

            First text

        Last text

        First description

        Middle description
        Last description

```

###  Find elements

[](#-find-elements)

Returns all found node elements.

Search by tag name:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('div');
```

Result:

```
Header-subheaderFirst textLast text
Header-subheaderFirst text
First descriptionMiddle descriptionLast description
```

Find by XPath:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('//div/div');
```

Result:

```
Header-subheaderFirst text
```

Find by attributes:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('//*[@class="content"]');
```

Result:

```
Header-subheaderFirst textLast text
```

###  First element

[](#-first-element)

Returns the first found node element.

The first item in the list:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('p')->first();
```

Result:

```
First text
```

First found elements in a node:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->first('p');
```

Result:

```
First text
```

First found elements in a node by XPath:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->first('//span[@class]');
```

Result:

```
First text
```

First found elements in a node:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('div')->first('p');
```

Result:

```
First text
First text
First description
```

###  Last elements

[](#-last-elements)

Returns the last found node element.

The last item in the list:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('p')->last();
```

Result:

```
Last description
```

Last found elements in a node:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->last('p');
```

Result:

```
Last description
```

Last found elements in a node by XPath:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->last('//span[@class]');
```

Result:

```
First text
```

Last found elements in a node:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('div')->last('p');
```

Result:

```
Last text
First text
Last description
```

###  N element

[](#-n-element)

Returns the element by index.

N list item:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('p')->getByPosition(1);
```

Result:

```
Last text
```

N found elements in a node:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->getByPosition(1, 'p');
```

Result:

```
Last text
```

N found elements in a node by XPath:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->getByPosition(2, '//*[@class]');
```

Result:

```

```

N found elements in a node:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('div')->getByPosition(1, 'p');
```

Result:

```
Last text
Last description
```

###  Closest

[](#-closest)

Returns the closest parent element.

Closest parent element by tag name:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('span')->closest('div');
```

Result:

```
Header-subheaderFirst text
Header-subheaderFirst text
First descriptionMiddle descriptionLast description
```

Parent element:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('span')->closest();
```

Result:

```
Header-subheader
First text
Last description
```

###  Parent

[](#-parent)

Returns the parent element.

Parent element:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('span')->parent();
```

Result:

```
Header-subheader
First text
Last description
```

###  Root

[](#-root)

Returns the root element.

Main parent:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('span')->root();
```

Result:

```
Header-subheaderFirst textLast textFirst descriptionMiddle descriptionLast description
```

###  Childes

[](#-childes)

Returns all children of the node.

Get the childes in a node:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('p')->childes();
```

Result:

```
First text
Last description
```

Find the childes in a node:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->childes('p');
```

Result:

```
First text
Last description
```

Find by XPath:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->childes('//*[@class="description"]/p');
```

Result:

```
Last description
```

###  First childes

[](#-first-childes)

Returns the first child of the node.

Get the first childes in a node:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('div')->firstChild();
```

Result:

```
Header-subheaderFirst text
Header-subheader
First description
```

Find the childes in a node:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('p')->firstChild();
```

Result:

```
First text
Last description
```

Find the childes in a node:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->firstChild('div');
```

Result:

```
First text
Last description
```

Find the childes in a node:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->firstChild('p');
```

Result:

```
Header-subheaderFirst text
Header-subheader
First description
```

Find by XPath:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->firstChild('//div/div');
```

Result:

```
Header-subheader
```

###  Get DOMNode

[](#-get-domnode)

Returns the DOMNode element by index.

Get an element by index:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('p')->getNodeByPosition(1);
```

Result:

```
class DOMElement {}
```

###  Find DOMNode

[](#-find-domnode)

Returns the found DOMNode element by index.

Find element:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->findNodeByPosition(1, 'p');
```

Result:

```
class DOMElement {}
```

###  Node name

[](#-node-name)

Returns node name.

Node name:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('div')->name();
```

Result:

```
div
div
div

```

###  Rename node

[](#-rename-node)

Rename node.

Rename current node:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('div')->rename('section');
```

Result:

```
Header-subheaderFirst textLast text
Header-subheaderFirst text
First descriptionMiddle descriptionLast description
```

Find and rename a node:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->rename('section', 'div');
```

```
Header-subheaderFirst textLast text
Header-subheaderFirst text
First descriptionMiddle descriptionLast description
```

###  Replace node

[](#-replace-node)

Replace node.

Replace current node.

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('div')->replace('HeaderContent');
```

```
HeaderContentHeaderContent
```

Finde nad replace a node.

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->replace('HeaderContent', 'div');
```

Result:

```
HeaderContentHeaderContent
```

Find by XPath and replace node.

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->replace('HeaderContent', '//div/div');
```

Result:

```
HeaderContentLast textFirst descriptionMiddle descriptionLast description
```

###  Remove node

[](#-remove-node)

Remove node.

Remove current node.

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('div')->remove();
```

Result:

```

```

Find and remove a node.

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->remove('div');
```

Result:

```

```

Find by XPath and remove node.

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->remove('//div/div');
```

Result:

```
Last textFirst descriptionMiddle descriptionLast description
```

###  Count

[](#-count)

Counting the number of elements.

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('p')->count();     // 4
$nodes->count('p');             // 4
$nodes->count('//*[@class]');   // 5
```

###  Element exist

[](#-element-exist)

Checking for node existence.

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('p')->exist();     // true
$nodes->exist('p');             // true
$nodes->exist('//*[@class]');   // true
$nodes->exist('hr');            // false
```

###  Element not exist

[](#-element-not-exist)

Checking for node not existence.

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('p')->notExist();      // false
$nodes->notExist('p');              // false
$nodes->notExist('//*[@class]');    // false
$nodes->notExist('hr');             // true
```

###  Is element

[](#-is-element)

Checking that the number of elements is 1.

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('img')->isElem();  // true
$nodes->find('p')->isElem();    // false
```

###  Is list

[](#-is-list)

Checks that the number of elements is more than 1.

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('img')->isList();  // false
$nodes->find('p')->isList();    // true
```

###  DomComment

[](#-domcomment)

Validate DOMComment. Use isElem. Parsing by default ignore the node DOMComment.

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('img')->isDomComment();  // false
$nodes->find('p')->isDomComment();    // false
```

###  DomElement

[](#-domelement)

Validate DomElement. Use isElem.

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('img')->isDomElement();        // true
$nodes->find('p')->isDomElement();          // false
$nodes->find('p')->first()->isDomElement(); // true
```

###  DomText

[](#-domtext)

Validate DomText. Use isElem. Parsing by default ignore the node DomText.

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('img')->isDomText();  // false
$nodes->find('p')->isDomText();    // false
```

###  Node type

[](#-node-type)

Node type. Parsing by default ignore the node type not 1.

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('img')->type();  // 1
$nodes->find('p')->type();    // 1\n1\n1\n1
```

###  Text

[](#-text)

Returns text.

Get the text of the current item:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('//h1/span')->text();              // Header
$nodes->find('h1')->text();                     // Header-subheader
$nodes->find('//*[@class="content"]')->text();  // Header-subheaderFirst textLast text
```

Get the text of the list:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('p')->text();
```

Result:

```
First text
Last text
First description
Last description

```

###  Add text

[](#-add-text)

Adds text to the end of a node.

Adding text to an empty element:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('//h1/i')->addText('add text');
```

Result:

```
add text
```

Adding text to the end of an element:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('h1')->addText('add text');
```

Result:

```
Header-subheaderadd text
```

Adding text to the list:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('p')->addText('add text');
```

Result:

```
First textadd text
Last textadd text
First descriptionadd text
Last descriptionadd text
```

###  Replace text

[](#-replace-text)

Replace text in a node.

Adding text to an empty element:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('//h1/i')->replaceText('change text');
```

Result:

```
add text
```

Replace text in a node:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('h1')->replaceText('change text');
```

Result:

```
Headerchange text
```

Replace text to the list:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('p')->replaceText('change text');
```

Result:

```
First textchange text
change text
change text
Last descriptionchange text
```

###  Remove text

[](#-remove-text)

Remove text in a node.

Remove text in a node:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('h1')->removeText();
```

Result:

```
Header
```

Remove text to the list:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('p')->replaceText('change text');
```

Result:

```
First text

Last description
```

###  Attribute

[](#-attribute)

Return attribute value.

Get list values:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('h1')->attr('class');
```

Result:

```
content
description

```

Get value:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('h1')->attr('role');
```

Result:

```
contentinfo

```

Default value:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('h1')->attr('role', 'default');
```

Result:

```
default
default
contentinfo

```

###  Has attribute

[](#-has-attribute)

Checking if an attribute exists.

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('img')->hasAttr('src');        // true
$nodes->find('i')->hasAttr('class');        // true
$nodes->find('p')->hasAttr('class');        // false
$nodes->find('div')->hasAttr('class');      // false
```

###  Set attribute

[](#-set-attribute)

Set attribute value.

Set attribute to the element:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$elem = $nodes->find('i');
$elem->setAttr('class', 'value');               //
$elem->setAttr('data-value', 'edit');           //
$elem->setAttr('class', 'icon value');          //
```

Set attribute to the list:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$elem = $nodes->find('p')->setAttr('class', 'text');
```

Result:

```
First text
Last text
First description
Last description
```

###  Remove attribute

[](#-remove-attribute)

Remove attribute.

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$elem = $nodes->find('span')->removeAttr('class');
```

Result:

```
Header
First text
Last description
```

###  Append child

[](#-append-child)

Adding child node.

Adding to an empty element:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('i')->appendChild('HeaderContent');
```

Result:

```
HeaderContent
```

Adding to the list:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('p')->appendChild('HeaderContent');
```

Result:

```
First textHeaderContent
Last textHeaderContent
First descriptionHeaderContent
Last descriptionHeaderContent
```

Find and add:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->appendChild('HeaderContent', 'p');
```

Result:

```
First textHeaderContent
Last textHeaderContent
First descriptionHeaderContent
Last descriptionHeaderContent
```

###  Replace childes

[](#-replace-childes)

Replace childes node.

Replacing all childes:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('h1')->replaceChildes('HeaderContent');
```

Result:

```
HeaderContentHeaderContent-subheader
```

Replacing children by node name:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('h1')->replaceChildes('HeaderContent', 'i');
```

Result:

```
HeaderContentHeader-subheader
```

###  Remove chides

[](#-remove-chides)

Remove childes node.

Removing all childes:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('h1')->removeChildes();
```

Result:

```
-subheader
```

Removing descendants by node name:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('h1')->removeChildes('i');
```

Result:

```
Header-subheader
```

###  DOMNode list

[](#-domnode-list)

Returns a list of DOMNode elements.

List:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('p')->toArray();
```

Result:

```
[
    class DOMElement {},
    class DOMElement {},
    class DOMElement {},
    class DOMElement {}
]
```

###  NodeList

[](#-nodelist)

Return NodeList.

NodeList:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('p')->toList();
```

Result:

```
class NodeList {}
```

###  Content

[](#-content)

Returns the markup of an element, including its content.

Get the content of the current element:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('h1')->toString();
```

Result:

```
Header-subheader
```

Find and get the content of the element:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->toString('h1');
```

Result:

```
Header-subheader
```

Find by XPath and get the content of the element:

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->toString('//div/p');
```

Result:

```
First text
Last text
First description
Last descriptiona
```

###  Foreach elements

[](#-foreach-elements)

Calls a callback function for each item.

```
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$elem = $nodes->find('div');
$elem->each(function (\cse\DOMManager\Nodes\Nodes $item) {
    if ($item->hasAttr('class')) {
        $item->removeAttr('class');
    }
    $item->addText('this div element');
});
echo $elem->root()->content();
```

Result:

```
Header-subheaderFirst textthis div elementLast textthis div elementFirst descriptionMiddle descriptionLast descriptionthis div element
```

Testing &amp; Code Coverage
---------------------------

[](#testing--code-coverage)

PHPUnit is used for unit testing. Unit tests ensure that class and methods does exactly what it is meant to do.

General PHPUnit documentation can be found at .

To run the PHPUnit unit tests, execute:

```
phpunit PATH/TO/PROJECT/tests/
```

If you want code coverage reports, use the following:

```
phpunit --coverage-html ./report PATH/TO/PROJECT/tests/
```

Used PHPUnit default config:

```
phpunit --configuration PATH/TO/PROJECT/phpunit.xml
```

Support project
---------------

[](#support-project)

Many thanks to those who are ready to help in the development of the project. You can help:

- Add a bug report or suggestion for improvement.
- Share code improvements by sending a Pull Request.
- Make a translation or optimize it for your country.
- Modify the documentation.
- Also any other help.

License
-------

[](#license)

This PHP library is open-source under the MIT license. Please see [License File](https://github.com/cs-eliseev/dom-manager/blob/master/LICENSE.md) for more information.

---

> GitHub [@cs-eliseev](https://github.com/cs-eliseev)

###  Health Score

15

—

LowBetter than 3% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community5

Small or concentrated contributor base

Maturity27

Early-stage or recently created project

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/d3b9e49d0ce3fd71c8eb31b7c14e48373dbb415a9d16950ce52d23bf89b2ae26?d=identicon)[ak\_eliseev](/maintainers/ak_eliseev)

### Embed Badge

![Health badge](/badges/cse-dom-manager/health.svg)

```
[![Health](https://phpackages.com/badges/cse-dom-manager/health.svg)](https://phpackages.com/packages/cse-dom-manager)
```

###  Alternatives

[solspace/craft-calendar

The most powerful event management and calendaring plugin!

1830.8k1](/packages/solspace-craft-calendar)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
