Download the PHP package ryangittings/didom without Composer
On this page you can find all versions of the php package ryangittings/didom. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ryangittings/didom
More information about ryangittings/didom
Files in ryangittings/didom
Package didom
Short Description Simple and fast HTML parser
License MIT
Homepage https://github.com/Imangazaliev/DiDOM
Informations about the package didom
DiDOM
DiDOM - simple and fast HTML parser.
Contents
- Installation
- Quick start
- Creating new document
- Search for elements
- Verify if element exists
- Supported selectors
- Output
- Creating a new element
- Getting the name of an element
- Getting parent element
- Getting sibling elements
- Getting the child elements
- Getting document
- Working with element attributes
- Comparing elements
- Adding a child element
- Replacing element
- Removing element
- Working with cache
- Miscellaneous
- Comparison with other parsers
Installation
To install DiDOM run the command:
composer require imangazaliev/didom
Quick start
Creating new document
DiDom allows to load HTML in several ways:
With constructor
The second parameter specifies if you need to load file. Default is false
.
With separate methods
There are two methods available for loading XML: loadXml
and loadXmlFile
.
These methods accept additional options:
Search for elements
DiDOM accepts CSS selector or XPath as an expression for search. You need to path expression as the first parameter, and specify its type in the second one (default type is Query::TYPE_CSS
):
With method find()
:
If the elements that match a given expression are found, then method returns an array of instances of DiDom\Element
, otherwise - an empty array. You could also get an array of DOMElement
objects. To get this, pass false
as the third parameter.
With magic method __invoke()
:
With method xpath()
:
You can do search inside an element:
Verify if element exists
To verify if element exist use has()
method:
If you need to check if element exist and then get it:
but it would be faster like this:
because in the first case it makes two requests.
Supported selectors
DiDom supports search by:
- tag
- class, ID, name and value of an attribute
- pseudo-classes:
- first-, last-, nth-child
- empty and not-empty
- contains
- has
Output
Getting HTML
With method html()
:
Casting to string:
Formatting HTML output
An element does not have format()
method, so if you need to output formatted HTML of the element, then first you have to convert it to a document:
Inner HTML
Document does not have the method innerHtml()
, therefore, if you need to get inner HTML of a document, convert it into an element first:
Getting XML
Getting content
Creating a new element
Creating an instance of the class
First parameter is a name of an attribute, the second one is its value (optional), the third one is element attributes (optional).
An example of creating an element with attributes:
An element can be created from an instance of the class DOMElement
:
Using the method createElement
Getting the name of an element
Getting parent element
Getting sibling elements
Getting the child elements
Getting document
Working with element attributes
Creating/updating an attribute
With method setAttribute
:
With method attr
:
With magic method __set
:
Getting value of an attribute
With method getAttribute
:
With method attr
:
With magic method __get
:
Returns null
if attribute is not found.
Verify if attribute exists
With method hasAttribute
:
With magic method __isset
:
Removing attribute:
With method removeAttribute
:
With magic method __unset
:
Comparing elements
Appending child elements
Adding a child element
Replacing element
Removing element
Working with cache
Cache is an array of XPath expressions, that were converted from CSS.
Getting from cache
Cache setting
Miscellaneous
preserveWhiteSpace
By default, whitespace preserving is disabled.
You can enable the preserveWhiteSpace
option before loading the document:
count
The count ()
method counts children that match the selector:
matches
Returns true
if the node matches the selector:
isElementNode
Checks whether an element is an element (DOMElement):
isTextNode
Checks whether an element is a text node (DOMText):
isCommentNode
Checks whether the element is a comment (DOMComment):