English | Русский
DOM Manager
A simple library for management the DOM (XML, HTML) document.
Project repository: https://github.com/cs-eliseev/dom-manager
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
You can find the most recent version of this project here.
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
Clone this repository locally:
git clone https://github.com/cs-eliseev/dom-manager.git
Download
Download the latest release here.
Usage
The following demonstrates how to use the functions of this library in a PHP application.
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
# | Method | Parameters | Return values | Description |
---|---|---|---|---|
Navigation methods | ||||
# | find($node_name) | $node_name - search parameter | Nodes | Returns all found node elements |
# | first($node_name) | $node_name - search parameter (optional) | Nodes | Returns the first found node element |
# | last($node_name) | $node_name - search parameter (optional) | Nodes | Returns the last found node element |
# | getByPosition($position, $node_name) | $position - number index, $node_name - search parameter (optional) | Nodes | Returns the element by index |
# | closest($node_name) | $node_name - parent element search parameter (optional) | Nodes | Returns the closest parent element |
# | parent() | Nodes | Returns the parent element | |
# | root() | Nodes | Returns the root element | |
# | childes($node_name) | $node_name - search parameter (optional) | Nodes | Returns all children of the node |
# | firstChild($node_name) | $node_name - search parameter (optional) | Nodes | Returns the first child of the node |
# | getNodeByPosition($position) | $position - number index | DOMNode | Returns the DOMNode element by index |
# | findNodeByPosition($position, $node_name) | $position - number index, $node_name - search parameter | Nodes | Returns the found DOMNode element by index |
Node management methods | ||||
# | name() | string | Returns node name | |
# | rename($new_node_name, $old_node_name) | $new_node_name - new node name, $old_node_name - search parameter (optional) | Nodes | Rename node |
# | replace($content, $node_name) | $content - replacement data, $node_name - search parameter (optional) | Nodes | Replace node |
# | remove($node_name) | $node_name - search parameter (optional) | Nodes | Remove node |
Node information | ||||
# | count($node_name) | $node_name - search parameter (optional) | int | Counting the number of elements |
# | exist($node_name) | $node_name - search parameter (optional) | bool | Checking for node existence |
# | notExist($node_name) | $node_name - search parameter (optional) | bool | Checking for node not existence |
# | isElem() | bool | Checking that the number of elements is 1 | |
# | isList() | bool | Checks that the number of elements is more than 1 | |
# | isDomComment() | bool | Validation DOMComment | |
# | isDomElement() | bool | Validation DOMElement | |
# | isDomText() | bool | Validation DOMText | |
# | type() | string | Return type | |
Methods for management text | ||||
# | text() | string | Returns text | |
# | addText($text) | $text - adding text | Nodes | Adds text to the end of a node |
# | replaceText($text) | $text - replace text | Nodes | Replacing text in a node |
# | removeText() | Nodes | Remove text in a node | |
Methods for management attributes | ||||
# | attr($attribute, $default) | $attribute - attribute name, $default - default value (optional) | string | Return attribute value |
# | hasAttr($attribute) | $attribute - attribute name | bool | Checking if an attribute exists |
# | setAttr($attribute, $value) | $attribute - attribute name, $value - value to insert | Nodes | Set attribute value |
# | removeAttr($attribute) | $attribute - attribute name | Nodes | Remove attribute |
Methods for management childes | ||||
# | appendChild($content, $node_name) | $content - replace data, $node_name - search parameter (optional) | Nodes | Adding child node |
# | replaceChildes($content, $node_name) | $content - replacement data, $node_name - search parameter (optional) | Nodes | Replace childes node |
# | removeChildes($node_name) | $node_name - search parameter (optional) | Nodes | Remove childes node |
Transform methods | ||||
# | toArray() | DOMNode[] | Returns a list of DOMNode elements | |
# | toList() | NodeList | Return NodeList | |
# | toString($node_name) | $node_name - search parameter (optional) | string | Returns the markup of an element, including its content. |
List iteration methods | ||||
# | each(function ($elem) {}) | Calls a callback function for each item |
Example
Example of document content:
Header-subheader
First text
Last text
First description
Middle description
Last description
<main class="main" role="main">
<div class="content">
<div>
<h1><i class="icon"/><span>Headerspan>-subheaderh1>
<img src="content_img.jpg" alt="content img"/>
<p><span class="text">First textspan>p>
div>
<br/>
<p>Last textp> div> <div class="description" role="contentinfo"> <p>First descriptionp> <br/> <em>Middle descriptionem> <p><span>Last descriptionspan>p> div> main>
Find elements
Returns all found node elements.
Search by tag name:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('div');
Result:
Header-subheader
First text
Last text
Header-subheader
First text
First description
Middle description
Last description
<div class="content"><div><h1><i class="icon"/><span>Headerspan>-subheaderh1><img src="content_img.jpg" alt="content img"/><p><span class="text">First textspan>p>div><br/><p>Last textp>div> <div><h1><i class="icon"/><span>Headerspan>-subheaderh1><img src="content_img.jpg" alt="content img"/><p><span class="text">First textspan>p>div> <div class="description" role="contentinfo"><p>First descriptionp><br/><em>Middle descriptionem><p><span>Last descriptionspan>p>div>
Find by XPath:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('//div/div');
Result:
Header-subheaderFirst text
"><div><h1><i class="icon"/><span>Headerspan>-subheaderh1><img src="content_img.jpg" alt="content img"/><p><span class="text">First textspan>p>div>
Find by attributes:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('//*[@class="content"]');
Result:
Header-subheader
First text
Last text
<div class="content"><div><h1><i class="icon"/><span>Headerspan>-subheaderh1><img src="content_img.jpg" alt="content img"/><p><span class="text">First textspan>p>div><br/><p>Last textp>div>
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:
<p><span class="text">First textspan>p>
First found elements in a node:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->first('p');
Result:
<p><span class="text">First textspan>p>
First found elements in a node by XPath:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->first('//span[@class]');
Result:
<span class="text">First textspan>
First found elements in a node:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('div')->first('p');
Result:
First text
First description
"><p><span class="text">First textspan>p>
<p><span class="text">First textspan>p>
<p>First descriptionp>
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:
<p><span>Last descriptionspan>p>
Last found elements in a node:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->last('p');
Result:
<p><span>Last descriptionspan>p>
Last found elements in a node by XPath:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->last('//span[@class]');
Result:
<span class="text">First textspan>
Last found elements in a node:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('div')->last('p');
Result:
Last description
"><p>Last textp>
<p><span class="text">First textspan>p>
<p><span>Last descriptionspan>p>
N element
Returns the element by index.
N list item:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('p')->getByPosition(1);
Result:
<p>Last textp>
N found elements in a node:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->getByPosition(1, 'p');
Result:
<p>Last textp>
N found elements in a node by XPath:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->getByPosition(2, '//*[@class]');
Result:
"><i class="icon"/>
N found elements in a node:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('div')->getByPosition(1, 'p');
Result:
<p>Last textp>
<p><span>Last descriptionspan>p>
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-subheader
First text
First description
Middle description
Last description
<div><h1><i class="icon"/><span>Headerspan>-subheaderh1><img src="content_img.jpg" alt="content img"/><p><span class="text">First textspan>p>div> <div><h1><i class="icon"/><span>Headerspan>-subheaderh1><img src="content_img.jpg" alt="content img"/><p><span class="text">First textspan>p>div> <div class="description" role="contentinfo"><p>First descriptionp><br/><em>Middle descriptionem><p><span>Last descriptionspan>p>div>
Parent element:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('span')->closest();
Result:
Header-subheaderFirst text
Last description
"><h1><i class="icon"/><span>Headerspan>-subheaderh1> <p><span class="text">First textspan>p> <p><span>Last descriptionspan>p>
Parent
Returns the parent element.
Parent element:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('span')->parent();
Result:
Header-subheaderFirst text
Last description
"><h1><i class="icon"/><span>Headerspan>-subheaderh1> <p><span class="text">First textspan>p> <p><span>Last descriptionspan>p>
Root
Returns the root element.
Main parent:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('span')->root();
Result:
Header-subheader
First text
Last text
First description
Middle description
Last description
<main class="main" role="main"><div class="content"><div><h1><i class="icon"/><span>Headerspan>-subheaderh1><img src="content_img.jpg" alt="content img"/><p><span class="text">First textspan>p>div><br/><p>Last textp>div><div class="description" role="contentinfo"><p>First descriptionp><br/><em>Middle descriptionem><p><span>Last descriptionspan>p>div>main>
Childes
Returns all children of the node.
Get the childes in a node:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('p')->childes();
Result:
<span class="text">First textspan>
<span>Last descriptionspan>
Find the childes in a node:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->childes('p');
Result:
<span class="text">First textspan>
<span>Last descriptionspan>
Find by XPath:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->childes('//*[@class="description"]/p');
Result:
<span>Last descriptionspan>
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
"><div><h1><i class="icon"/><span>Headerspan>-subheaderh1><img src="content_img.jpg" alt="content img"/><p><span class="text">First textspan>p>div> <h1><i class="icon"/><span>Headerspan>-subheaderh1> <p>First descriptionp>
Find the childes in a node:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('p')->firstChild();
Result:
<span class="text">First textspan>
<span>Last descriptionspan>
Find the childes in a node:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->firstChild('div');
Result:
<span class="text">First textspan>
<span>Last descriptionspan>
Find the childes in a node:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->firstChild('p');
Result:
Header-subheaderFirst text
Header-subheader
First description
"><div><h1><i class="icon"/><span>Headerspan>-subheaderh1><img src="content_img.jpg" alt="content img"/><p><span class="text">First textspan>p>div> <h1><i class="icon"/><span>Headerspan>-subheaderh1> <p>First descriptionp>
Find by XPath:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->firstChild('//div/div');
Result:
Header-subheader"><h1><i class="icon"/><span>Headerspan>-subheaderh1>
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
Returns the found DOMNode element by index.
Find element:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->findNodeByPosition(1, 'p');
Result:
class DOMElement {}
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 current node:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('div')->rename('section');
Result:
Header-subheader
First text
Last text
Header-subheader
First text
First description
Middle description
Last description
<section class="content"><section><h1><i class="icon"/><span>Headerspan>-subheaderh1><img src="content_img.jpg" alt="content img"/><p><span class="text">First textspan>p>section><br/><p>Last textp>section> <section><h1><i class="icon"/><span>Headerspan>-subheaderh1><img src="content_img.jpg" alt="content img"/><p><span class="text">First textspan>p>section> <section class="description" role="contentinfo"><p>First descriptionp><br/><em>Middle descriptionem><p><span>Last descriptionspan>p>section>
Find and rename a node:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->rename('section', 'div');
Header-subheader
First text
Last text
Header-subheader
First text
First description
Middle description
Last description
<div class="content"><div><h1><i class="icon"/><strong>Headerstrong>-subheaderh1><img src="content_img.jpg" alt="content img"/><p><strong class="text">First textstrong>p>div><br/><p>Last textp>div> <div><h1><i class="icon"/><strong>Headerstrong>-subheaderh1><img src="content_img.jpg" alt="content img"/><p><strong class="text">First textstrong>p>div> <div class="description" role="contentinfo"><p>First descriptionp><br/><em>Middle descriptionem><p><strong>Last descriptionstrong>p>div>
Replace node
Replace node.
Replace current node.
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('div')->replace('
Header
Content
');
<main class="main" role="main"><section><div>Headerdiv>Contentsection><section><div>Headerdiv>Contentsection>main>
Finde nad replace a node.
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->replace('
Header
Content
', 'div');
Result:
<main class="main" role="main"><section><div>Headerdiv>Contentsection><section><div>Headerdiv>Contentsection>main>
Find by XPath and replace node.
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->replace('
Header
Content
', '//div/div');
Result:
Last text
First description
Middle description
Last description
<main class="main" role="main"><div class="content"><section><div>Headerdiv>Contentsection><br/><p>Last textp>div><div class="description" role="contentinfo"><p>First descriptionp><br/><em>Middle descriptionem><p><span>Last descriptionspan>p>div>main>
Remove node
Remove node.
Remove current node.
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('div')->remove();
Result:
"><main class="main" role="main"/>
Find and remove a node.
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->remove('div');
Result:
"><main class="main" role="main"/>
Find by XPath and remove node.
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->remove('//div/div');
Result:
Last text
First description
Middle description
Last description
<main class="main" role="main"><div class="content"><br/><p>Last textp>div><div class="description" role="contentinfo"><p>First descriptionp><br/><em>Middle descriptionem><p><span>Last descriptionspan>p>div>main>
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
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
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
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
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
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
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
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. 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
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
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:
<i class="icon">add texti>
Adding text to the end of an element:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('h1')->addText('add text');
Result:
Header-subheaderadd text"><h1><i class="icon"/><span>Headerspan>-subheaderadd texth1>
Adding text to the list:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('p')->addText('add text');
Result:
Last textadd text
First descriptionadd text
Last descriptionadd text
"><p><span class="text">First textspan>add textp>
<p>Last textadd textp>
<p>First descriptionadd textp>
<p><span>Last descriptionspan>add textp>
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:
<i class="icon">add texti>
Replace text in a node:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('h1')->replaceText('change text');
Result:
Headerchange text"><h1><i class="icon"/><span>Headerspan>change texth1>
Replace text to the list:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('p')->replaceText('change text');
Result:
change text
change text
Last descriptionchange text
"><p><span class="text">First textspan>change textp>
<p>change textp>
<p>change textp>
<p><span>Last descriptionspan>change textp>
Remove text
Remove text in a node.
Remove text in a node:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('h1')->removeText();
Result:
Header"><h1><i class="icon"/><span>Headerspan>h1>
Remove text to the list:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('p')->replaceText('change text');
Result:
Last description
"><p><span class="text">First textspan>p>
<p>p>
<p>p>
<p><span>Last descriptionspan>p>
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
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 value.
Set attribute to the element:
$elem->setAttr('data-value', 'edit'); // $elem->setAttr('class', 'icon value'); // ">/** @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:
Last text
First description
Last description
"><p class="text"><span class="text">First textspan>p>
<p class="text">Last textp>
<p class="text">First descriptionp>
<p class="text"><span>Last descriptionspan>p>
Remove attribute
Remove attribute.
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$elem = $nodes->find('span')->removeAttr('class');
Result:
<span>Headerspan>
<span>First textspan>
<span>Last descriptionspan>
Append child
Adding child node.
Adding to an empty element:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('i')->appendChild('
Header
Content
');
Result:
<i class="icon"><section><div>Headerdiv>Contentsection>i>
Adding to the list:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('p')->appendChild('
Header
Content
');
Result:
Last text
First description
Last description
<p><span class="text">First textspan><section><div>Headerdiv>Contentsection>p>
<p>Last text<section><div>Headerdiv>Contentsection>p> <p>First description<section><div>Headerdiv>Contentsection>p> <p><span>Last descriptionspan><section><div>Headerdiv>Contentsection>p>
Find and add:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->appendChild('
Header
Content
', 'p');
Result:
Last text
First description
Last description
<p><span class="text">First textspan><section><div>Headerdiv>Contentsection>p>
<p>Last text<section><div>Headerdiv>Contentsection>p> <p>First description<section><div>Headerdiv>Contentsection>p> <p><span>Last descriptionspan><section><div>Headerdiv>Contentsection>p>
Replace childes
Replace childes node.
Replacing all childes:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('h1')->replaceChildes('
Header
Content
');
Result:
<h1><section><div>Headerdiv>Contentsection><section><div>Headerdiv>Contentsection>-subheaderh1>
Replacing children by node name:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('h1')->replaceChildes('
Header
Content
', 'i');
Result:
<h1><section><div>Headerdiv>Contentsection><span>Headerspan>-subheaderh1>
Remove chides
Remove childes node.
Removing all childes:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('h1')->removeChildes();
Result:
<h1>-subheaderh1>
Removing descendants by node name:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('h1')->removeChildes('i');
Result:
<h1><span>Headerspan>-subheaderh1>
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
Return NodeList.
NodeList:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->find('p')->toList();
Result:
class NodeList {}
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"><h1><i class="icon"/><span>Headerspan>-subheaderh1>
Find and get the content of the element:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->toString('h1');
Result:
Header-subheader"><h1><i class="icon"/><span>Headerspan>-subheaderh1>
Find by XPath and get the content of the element:
/** @var \cse\DOMManager\Nodes\Nodes $nodes */
$nodes->toString('//div/p');
Result:
Last text
First description
Last description
a"><p><span class="text">First textspan>p>
<p>Last textp>
<p>First descriptionp>
<p><span>Last descriptionspan>p>a
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-subheader
First text
this div elementLast text
this div elementFirst description
Middle description
Last description
this div element<main class="main" role="main"><div><div><h1><i class="icon"/><span>Headerspan>-subheaderh1><img src="content_img.jpg" alt="content img"/><p><span class="text">First textspan>p>this div elementdiv><br/><p>Last textp>this div elementdiv><div role="contentinfo"><p>First descriptionp><br/><em>Middle descriptionem><p><span>Last descriptionspan>p>this div elementdiv>main>
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 https://phpunit.de/documentation.html.
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
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
This PHP library is open-source under the MIT license. Please see License File for more information.
GitHub @cs-eliseev