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

Overview

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

content img

First text


Last text

">
<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

content img

First text


Last text

Header-subheader

content img

First 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> <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-subheader content img

First 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

content img

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:

First text

">
<p><span class="text">First textspan>p>

First found elements in a node:

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

Result:

First text

">
<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:

First text">
<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 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:

First text">
<span class="text">First textspan>

Last found elements in a node:

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

Result:

First text

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-subheader content img

First text

Header-subheader

content img

First 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>
<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-subheader

First 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-subheader

First 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

content img

First text


Last text

">
<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:

First text Last description">
<span class="text">First textspan>
<span>Last descriptionspan>

Find the childes in a node:

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

Result:

First text Last description">
<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-subheader content img

First 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:

First text Last description">
<span class="text">First textspan>
<span>Last descriptionspan>

Find the childes in a node:

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

Result:

First text Last description">
<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-subheader content img

First 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

content img

First text


Last text

Header-subheader

content img

First text

">
<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

content img

First text


Last text

Header-subheader

content img

First text

">
<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
'
);
Header
Content
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:

Header
Content
Header
Content
">
<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:

Header
Content

Last text

">
<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

">
<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:

text(); // Header-subheaderFirst textLast text">
/** @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:

add text">
<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:

First textadd text

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:

add text">
<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:

First textchange text

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:

First text

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:

First text

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:

Header
Content
">
<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:

First text
Header
Content

Last text

Header
Content

First description

Header
Content

Last description

Header
Content

">
<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:

First text
Header
Content

Last text

Header
Content

First description

Header
Content

Last description

Header
Content

">
<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:

First text

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

content img

First text

this div element

Last text

this div element

First 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

You might also like...
The easiest way to match data structures like JSON/PlainText/XML against readable patterns. Sandbox:

PHP Matcher Library created for testing all kinds of JSON/XML/TXT/Scalar values against patterns. API: PHPMatcher::match($value = '{"foo": "bar"}', $p

Rah sitemap - XML sitemap generator for Textpattern CMS

rah_sitemap Packagist | Issues | Donate Sitemap plugin for Textpattern CMS. Generates Sitemaps.org XML sitemaps for your site, which help Google and o

A great Start for your next Magento Theme's local.xml file

Magento-local.xml-Template A Great Start for your next Magento Theme's local.xml file - ?xml version="1.0"? layout !-- Add/Remove Items From H

Improve default Magento 2 Import / Export features - cron jobs, CSV , XML , JSON , Excel
Improve default Magento 2 Import / Export features - cron jobs, CSV , XML , JSON , Excel

Improve default Magento 2 Import / Export features - cron jobs, CSV , XML , JSON , Excel , mapping of any format, Google Sheet, data and price modification, improved speed and a lot more!

Sistema disema con aplicación de consultas en XML y JSON

disema-XML-JSON Sistema web para empresa de diseño "Disema", con operaciones básicas CRUD y uso de html, JQ, JS, php y css. Incluye aplicación de cons

laminas-xml2json provides functionality for converting XML structures to JSON

laminas-xml2json This package is considered feature-complete, and is now in security-only maintenance mode, following a decision by the Technical Stee

An utility component for XML usage and best practices in PHP

An utility component for XML usage and best practices in PHP

PluXml, Moteur de Blog et CMS à l'XML sans base de données

PluXml Créez un site web performant en toute simplicité et sans base de données. Télécharger PluXml 5.8.7 (zip) Version bugfix (5.8.8) en développemen

A small CLI tool to check missing dependency declarations in the composer.json and module.xml

Integrity checker Package allows to run static analysis on Magento 2 Module Packages to provide an integrity check of package. Supported tools: Compos

Owner
Alexey
Alexey
The main website source code based on php , html/css/js and an independent db system using xml/json.

jsm33t.com Well umm, a neat website LIVE SITE » View Demo · Report Bug · Request a feature About The Project Desc.. Built Using Php UI Frameworks Boot

Jasmeet Singh 5 Nov 23, 2022
html-sanitizer is a library aiming at handling, cleaning and sanitizing HTML sent by external users

html-sanitizer html-sanitizer is a library aiming at handling, cleaning and sanitizing HTML sent by external users (who you cannot trust), allowing yo

Titouan Galopin 381 Dec 12, 2022
Okex API Like the official document interface, Support for arbitrary extension.

It is recommended that you read the official document first Okex docs https://www.okex.com/docs/en Okex Simulation Test API https://www.okex.com/docs/

lin 34 Jan 1, 2023
Declaratively specify how to extract elements from a JSON document, in PHP

jmespath.php JMESPath (pronounced "jaymz path") allows you to declaratively specify how to extract elements from a JSON document. jmespath.php allows

null 1.7k Dec 30, 2022
A PHP package for MRZ (Machine Readable Zones) code parser for Passport, Visa & Travel Document (TD1 & TD2).

MRZ (Machine Readable Zones) Parser for PHP A PHP package for MRZ (Machine Readable Zones) code parser for Passport, Visa & Travel Document (TD1 & TD2

Md. Rakibul Islam 25 Aug 24, 2022
This document provides the details related to Remittance API. This APIs is used to initiate payment request from Mobile client/others exchange house.

City Bank Remittance API This is where your description should go. Limit it to a paragraph or two. Consider adding a small example. Installation You c

MD ARIFUL HAQUE 2 Oct 2, 2022
PHP library for working with Demandware XML files

PHP Demandware XML A PHP library for working with Demandware XML files. Exporting: Supports category, product, variant and assignment files and allows

Fusions PIM 3 Dec 14, 2022
Simple XML-RPC API

Readme Ripcord: Easy XML-RPC Client and Server for PHP 5 ========================================================================

Saravanakumar Arumugam 1 Nov 22, 2021
Sanitize untrustworthy HTML user input (Symfony integration for https://github.com/tgalopin/html-sanitizer)

html-sanitizer is a library aiming at handling, cleaning and sanitizing HTML sent by external users (who you cannot trust), allowing you to store it and display it safely. It has sensible defaults to provide a great developer experience while still being entierely configurable.

Titouan Galopin 86 Oct 5, 2022
[READ-ONLY] CakePHP Utility classes such as Inflector, Text, Hash, Security and Xml. This repo is a split of the main code that can be found in https://github.com/cakephp/cakephp

CakePHP Utility Classes This library provides a range of utility classes that are used throughout the CakePHP framework What's in the toolbox? Hash A

CakePHP 112 Feb 15, 2022