Skip to content
This repository has been archived by the owner on Apr 19, 2022. It is now read-only.
/ DOMQuery Public archive

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

Notifications You must be signed in to change notification settings

artack/DOMQuery

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

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

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages