artack/dom-query

This package is abandoned and no longer maintained. No replacement package was suggested.
There is no license information available for the latest version (0.3.0) of this package.

DOMQuery is a PHP library that allows to easily traverse and modify the DOM

0.3.0 2019-02-27 13:05 UTC

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()