ATTENTION: This repository is archived and therefore readonly., (*1)
DOMQuery
DOMQuery is a PHP library that allows to easily traverse and modify the DOM, (*2)
Installation
Add in your composer.json:, (*3)
{
"require": {
"artack/dom-query": "*"
}
}
Running the command:, (*4)
``` bash
$ php composer.phar update artack/dom-query, (*5)
Usage
-------
### Tree Traversal
``` php
$q = DOMQuery::create('
'
);
//output: 2
$q->getChildren()->count()
//output: 3
$q->find('li')->count()
//output: <li class="first">first</li>
$q->find('li')->getFirst()->getHtml()
//output: <li class="last">last</li>
$q->find('li')->getLast()->getHtml()
//output: <li class="second">second</li>
$q->find('li')->filter('.second')->getHtml()
//output: <li class="second">second</li>
$q->find('li')->get(1)->getHtml()
//output: ul
$q->find('li')->getParent()->getName()
DOM Manipulation
``` php, (*6)
//output:, (*7)
Title
Text
DOMQuery::create(', (*8)
Title
')
->append('Text')
->getHtml(), (*9)
//output:, (*10)
Text
Title
DOMQuery::create(', (*11)
Title
')
->prepend('Text')
->getHtml(), (*12)
//output:, (*13)
Title H2
DOMQuery::create(', (*14)
Title
')
->find('h1')
->replace(', (*15)
Title H2
')
->getParent()
->getHtml(), (*16)
//output:, (*17)
New Title
DOMQuery::create(', (*18)
Title
')
->find('h1')
->replaceInner('New Title')
->getParent()
->getHtml(), (*19)
### Attribute Manipulation
``` php
//output: <img src="image.jpg" style="width:12px;" class="image">
DOMQuery::create('<img>')
->setAttribute('src', 'image.jpg')
->setStyle('width', '12px')
->addClass('image')
->getHtml()
HTML Output
``` php
//output:, (*20)
Title
DOMQuery::create(', (*21)
Title
')->getHtml(), (*22)
//output: Title
DOMQuery::create(', (*23)
Title
')->getInnerHtml()
```, (*24)