2017 © Pedro Peláez
 

library dom-query

DomQuery is a PHP library that allows easy 'jQuery like' DOM traversing and manipulation

image

rct567/dom-query

DomQuery is a PHP library that allows easy 'jQuery like' DOM traversing and manipulation

  • Monday, March 12, 2018
  • by Rct567
  • Repository
  • 1 Watchers
  • 8 Stars
  • 115 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 27 Versions
  • 20 % Grown

The README.md

DomQuery

DomQuery is a PHP library that allows you to easily traverse and modify the DOM (HTML/XML). As a library it aims to provide 'jQuery like' access to the PHP DOMDocument class (http://php.net/manual/en/book.dom.php)., (*1)

Installation

Install the latest version with, (*2)

$ composer require rct567/dom-query

Basic Usage

Read attributes and properties:

``` php use Rct567\DomQuery\DomQuery;, (*3)

$dom = new DomQuery(', (*4)

Hello

');, (*5)

echo $dom->find('h1')->text(); // output: Hello echo $dom->find('div')->prop('outerHTML'); // output:, (*6)

Hello

echo $dom->find('div')->html(); // output:, (*7)

Hello

echo $dom->find('div > h1')->class; // output: title echo $dom->find('div > h1')->attr('class'); // output: title echo $dom->find('div > h1')->prop('tagName'); // output: h1 echo $dom->find('div')->children('h1')->prop('tagName'); // output: h1 echo (string) $dom->find('div > h1'); // output:, (*8)

Hello

echo count($dom->find('div, h1')); // output: 2, (*9)


### Traversing nodes (result set): ``` php use Rct567\DomQuery\DomQuery; $dom = new DomQuery('<a>1</a> <a>2</a> <a>3</a>'); $links = $dom->children('a'); foreach($links as $elm) { echo $elm->text(); // output 123 } echo $links[0]->text(); // output 1 echo $links->last()->text(); // output 3 echo $links->first()->next()->text(); // output 2 echo $links->last()->prev()->text(); // output 2 echo $links->get(0)->textContent; // output 1 echo $links->get(-1)->textContent; // output 3

Factory method (create instance alternative):

use Rct567\DomQuery\DomQuery;

DomQuery::create('<a title="hello"></a>')->attr('title') // hello

Jquery methods available

Traversing > Tree Traversal

  • .find( selector )
  • .children( [selector] )
  • .parent( [selector] )
  • .closest( [selector] )
  • .next( [selector] )
  • .prev( [selector] )
  • .nextAll( [selector] )
  • .prevAll( [selector] )
  • .nextUntil( [selector] )
  • .prevUntil( [selector] )
  • .siblings( [selector] )

Traversing > Miscellaneous Traversing

  • .contents() get children including text nodes
  • .add( selector, [context] ) new result with added elements that match selector
  • .addBack(), (*10)

    Traversing > Filtering

  • .is( selector ), (*11)

  • .filter ( selector ) reduce to those that match the selector
  • .not( selector ) remove elements from the set of matched elements
  • .has( selector ) reduce to those that have a descendant that matches the selector
  • .first( [selector] )
  • .last( [selector] )
  • .slice( [offset] [, length]) like array_slice in php, not js/jquery
  • .eq( index )
  • .map( callable(elm,i) )

* [selector] can be a css selector or an instance of DomQuery|DOMNodeList|DOMNode , (*12)

#### Manipulation > DOM Insertion & removal, (*13)

  • .text( [text] )
  • .html( [html_string] )
  • .append( [content],... )
  • .prepend( [content],... )
  • .after( [content],... )
  • .before( [content],... )
  • .appendTo( [target] )
  • .prependTo( [target] )
  • .replaceWith( [content] )
  • .wrap( [content] )
  • .wrapAll( [content] )
  • .wrapInner( [content] )
  • .remove( [selector] )

* [content] can be html or an instance of DomQuery|DOMNodeList|DOMNode, (*14)

#### Attributes | Manipulation, (*15)

  • .attr( name [, val] )
  • .prop( name [, val] )
  • .css( name [, val] )
  • .removeAttr( name )
  • .addClass( name )
  • .hasClass( name )
  • .toggleClass ( name )
  • .removeClass( [name] )

* addClass, removeClass, toggleClass and removeAttr also accepts an array or space-separated __names__, (*16)

#### Miscellaneous > DOM Element Methods | Traversing | Storage, (*17)

  • .get( index )
  • .each ( callable(elm,i) )
  • .data ( key [, val] )
  • .removeData ( [name] )
  • .index ( [selector] )
  • .toArray()
  • .clone()

Supported selectors

  • .class
  • #foo
  • parent > child
  • foo, bar multiple selectors
  • prev + next elements matching "next" that are immediately preceded by a sibling "prev"
  • prev ~ siblings elements matching "siblings" that are preceded by "prev"
  • * all selector
  • [name="foo"] attribute value equal foo
  • [name*="foo"] attribute value contains foo
  • [name~="foo"] attribute value contains word foo
  • [name^="foo"] attribute value starts with foo
  • [name$="foo"] attribute value ends with foo
  • [name|="foo"] attribute value equal to foo, or starting foo followed by a hyphen (-)

Pseudo selectors

  • :empty
  • :even
  • :odd
  • :first-child
  • :last-child
  • :only-child
  • :nth-child(n)
  • :parent elements that have at least one child node
  • :first
  • :last
  • :header selects h1, h2, h3 etc.
  • :not(foo) elements that do not match selector foo
  • :has(foo) elements containing at least one element that matches foo selector
  • :contains(foo) elements that contain text foo
  • :root element that is the root of the document

Other (non jQuery) methods

  • findOrFail( selector ) find descendants of each element in the current set of matched elements, or throw an exception
  • loadContent(content, encoding='UTF-8') load html/xml content
  • xpath(xpath_query) Use xpath to find descendants of each element in the current set of matched elements
  • getOuterHtml() get resulting html describing all the elements (same as (string) $dom, or $elm->prop('outerHTML'))

XML support

  • XML content will automatically be loaded 'as XML' if a XML declaration is found (property xml_mode will be set to true)
  • This in turn will also make saving (rendering) happen 'as XML'. You can set property xml_mode to false to prevent this.
  • To prevent content with a XML declaration loading 'as XML' you can set property xml_mode to false and then use the loadContent($content) method.
  • Namespaces are automatically registered (no need to do it manually)

Escaping meta chars in selector to find elements with namespace:, (*18)

$dom->find('namespace\\:h1')->text();

About

Requirements

  • Works with PHP 7.2 or above (try v0.8 for older PHP versions)
  • Requires libxml PHP extension (enabled by default)

Inspiration/acknowledgements

  • https://github.com/wasinger/htmlpagedom
  • https://github.com/symfony/dom-crawler
  • https://github.com/ARTACK/DOMQuery
  • https://github.com/zendframework/zend-dom
  • http://simplehtmldom.sourceforge.net

The Versions

12/03 2018

dev-master

9999999-dev

DomQuery is a PHP library that allows easy 'jQuery like' DOM traversing and manipulation

  Sources   Download

MIT

The Requires

  • php ^7.0.0
  • ext-xml *

 

by Avatar Rct567

css xml jquery html dom selector

12/03 2018

v0.6.1

0.6.1.0

DomQuery is a PHP library that allows easy 'jQuery like' DOM traversing and manipulation

  Sources   Download

MIT

The Requires

  • php ^7.0.0
  • ext-xml *

 

by Avatar Rct567

css xml jquery html dom selector

24/02 2018

v0.6

0.6.0.0

DomQuery is a PHP library that allows easy 'jQuery like' DOM traversing and manipulation

  Sources   Download

MIT

The Requires

  • php ^7.0.0
  • ext-xml *

 

by Avatar Rct567

css xml jquery html dom selector

13/01 2018

v0.5.7.1

0.5.7.1

DomQuery is a PHP library that allows easy 'jQuery like' DOM traversing and manipulation

  Sources   Download

MIT

The Requires

  • php ^7.0.0
  • ext-xml *

 

by Avatar Rct567

css xml jquery html dom selector

13/01 2018

v0.5.7.2

0.5.7.2

DomQuery is a PHP library that allows easy 'jQuery like' DOM traversing and manipulation

  Sources   Download

MIT

The Requires

  • php ^7.0.0
  • ext-xml *

 

by Avatar Rct567

css xml jquery html dom selector

13/01 2018

v0.5.7

0.5.7.0

DomQuery is a PHP library that allows easy 'jQuery like' DOM traversing and manipulation

  Sources   Download

MIT

The Requires

  • php ^7.0.0
  • ext-xml *

 

by Avatar Rct567

css xml jquery html dom selector

02/12 2017

v0.5.6

0.5.6.0

DomQuery is a PHP library that allows easy 'jQuery like' DOM traversing and manipulation

  Sources   Download

MIT

The Requires

  • php ^7.0.0
  • ext-xml *

 

by Avatar Rct567

css xml jquery html dom selector

15/11 2017

v0.5.5

0.5.5.0

DomQuery is a PHP library that allows easy 'jQuery like' DOM traversing and manipulation

  Sources   Download

MIT

The Requires

  • php ^7.0.0
  • ext-xml *

 

by Avatar Rct567

css xml jquery html dom selector

13/11 2017

v0.5.4

0.5.4.0

DomQuery is a PHP library that allows easy 'jQuery like' DOM traversing and manipulation

  Sources   Download

MIT

The Requires

  • php ^7.0.0
  • ext-xml *

 

by Avatar Rct567

css xml jquery html dom selector

12/11 2017

v0.5.3

0.5.3.0

DomQuery is a PHP library that allows easy 'jQuery like' DOM traversing and manipulation

  Sources   Download

MIT

The Requires

  • php ^7.0.0
  • ext-xml *

 

by Avatar Rct567

css xml jquery html dom selector

12/11 2017

v0.5.2

0.5.2.0

DomQuery is a PHP library that allows easy 'jQuery like' DOM traversing and manipulation

  Sources   Download

MIT

The Requires

  • php ^7.0.0
  • ext-xml *

 

by Avatar Rct567

css xml jquery html dom selector

11/11 2017

v0.5.1

0.5.1.0

DomQuery is a PHP library that allows easy 'jQuery like' DOM traversing and manipulation

  Sources   Download

MIT

The Requires

  • php ^7.0.0
  • ext-xml *

 

by Avatar Rct567

css xml jquery html dom selector

09/11 2017

v0.5.0

0.5.0.0

DomQuery is a PHP library that allows easy 'jQuery like' DOM traversing and manipulation

  Sources   Download

MIT

The Requires

  • php ^7.0.0
  • ext-xml *

 

by Avatar Rct567

css xml jquery html dom selector

04/11 2017

v0.4.2

0.4.2.0

DomQuery is a PHP library that allows easy 'jQuery like' DOM traversing and manipulation

  Sources   Download

MIT

The Requires

  • php ^7.0.0
  • ext-xml *

 

by Avatar Rct567

css xml jquery html dom selector

01/11 2017

v0.4.1

0.4.1.0

DomQuery is a PHP library that allows easy 'jQuery like' DOM traversing and manipulation

  Sources   Download

MIT

The Requires

  • php ^7.0.0
  • ext-xml *

 

by Avatar Rct567

css xml jquery html dom selector

30/10 2017

v0.4.0

0.4.0.0

DomQuery is a PHP library that allows easy 'jQuery like' DOM traversing and manipulation

  Sources   Download

MIT

The Requires

  • php ^7.0.0
  • ext-xml *

 

by Avatar Rct567

css xml jquery html dom selector

28/10 2017

v0.3.0

0.3.0.0

DomQuery is a PHP library that allows easy 'jQuery like' DOM traversing and manipulation

  Sources   Download

MIT

The Requires

  • php ^7.0.0
  • ext-xml *

 

by Avatar Rct567

css xml jquery html dom selector

27/10 2017

v0.2.0

0.2.0.0

DomQuery is a PHP library that allows easy 'jQuery like' DOM traversing and manipulation

  Sources   Download

MIT

The Requires

  • php ^7.0.0
  • ext-xml *

 

by Avatar Rct567

css xml jquery html dom selector

22/10 2017

v0.1.8

0.1.8.0

DomQuery is a PHP library that allows easy 'jQuery like' DOM traversing and manipulation

  Sources   Download

MIT

The Requires

  • php ^7.0.0
  • ext-xml *

 

by Avatar Rct567

xml jquery html dom

21/10 2017

v0.1.7

0.1.7.0

DomQuery is a PHP library that allows easy 'jQuery like' DOM traversing and manipulation

  Sources   Download

MIT

The Requires

  • php ^7.0.0
  • ext-xml *

 

by Avatar Rct567

xml jquery html dom

19/10 2017

v0.1.6

0.1.6.0

DomQuery is a PHP library that allows easy 'jQuery like' DOM traversing and manipulation

  Sources   Download

MIT

The Requires

  • php ^7.0.0
  • ext-xml *

 

by Avatar Rct567

xml jquery html dom

13/10 2017

v0.1.5

0.1.5.0

DomQuery is a PHP library that allows easy 'jQuery like' DOM traversing and manipulation

  Sources   Download

MIT

The Requires

  • php ^7.0.0
  • ext-xml *

 

by Avatar Rct567

xml jquery html dom

11/10 2017

v0.1.4

0.1.4.0

DomQuery is a PHP library that allows easy 'jQuery like' DOM traversing and manipulation

  Sources   Download

MIT

The Requires

  • php ^7.0.0
  • ext-xml *

 

by Avatar Rct567

xml jquery html dom

10/10 2017

v0.1.3

0.1.3.0

DomQuery is a PHP library that allows easy 'jQuery like' DOM traversing and manipulation

  Sources   Download

MIT

by Avatar Rct567

xml jquery html dom

08/10 2017

v0.1.2

0.1.2.0

DomQuery is a PHP library that allows easy 'jQuery like' DOM traversing and manipulation

  Sources   Download

MIT

by Avatar Rct567

xml jquery html dom

06/10 2017

v0.1.1

0.1.1.0

DomQuery is a PHP library that allows easy 'jQuery like' DOM traversing and manipulation

  Sources   Download

MIT

by Avatar Rct567

xml jquery html dom

06/10 2017

v0.1.0

0.1.0.0

DomQuery is a PHP library that allows easy 'jQuery like' DOM traversing and manipulation

  Sources   Download

MIT

by Avatar Rct567

xml jquery html dom