artack / dom-query
DOMQuery is a PHP library that allows to easily traverse and modify the DOM
Installs: 6 003
Dependents: 1
Suggesters: 0
Security: 0
Stars: 8
Watchers: 3
Forks: 1
Open Issues: 0
Requires
- php: >=5.4
- ext-dom: *
- symfony/css-selector: 3.*|4.*
This package is auto-updated.
Last update: 2022-05-19 08:59:54 UTC
README
ATTENTION: This repository is archived and therefore readonly.
DOMQuery
DOMQuery is a PHP library that allows to easily traverse and modify the DOM
Installation
Add in your composer.json:
{ "require": { "artack/dom-query": "*" } }
Running the command:
$ php composer.phar update artack/dom-query
Usage
Tree Traversal
$q = DOMQuery::create(' <div> <h1>Title</h1> <ul> <li class="first">first</li> <li class="second">second</li> <li class="last">last</li> </ul> </div>' ); //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
//output: <div><h1>Title</h1><span>Text</span></div> DOMQuery::create('<div><h1>Title</h1></div>') ->append('<span>Text</span>') ->getHtml() //output: <div><span>Text</span><h1>Title</h1></div> DOMQuery::create('<div><h1>Title</h1></div>') ->prepend('<span>Text</span>') ->getHtml() //output: <div><h2>Title H2</h2></div> DOMQuery::create('<div><h1>Title</h1></div>') ->find('h1') ->replace('<h2>Title H2</h2>') ->getParent() ->getHtml() //output: <div><h2>New Title</h2></div> DOMQuery::create('<div><h1>Title</h1></div>') ->find('h1') ->replaceInner('New Title') ->getParent() ->getHtml()
Attribute Manipulation
//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
//output: <h1>Title</h1> DOMQuery::create('<h1>Title</h1>')->getHtml() //output: Title DOMQuery::create('<h1>Title</h1>')->getInnerHtml()