Download the PHP package imangazaliev/didom without Composer
On this page you can find all versions of the php package imangazaliev/didom. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download imangazaliev/didom
More information about imangazaliev/didom
Files in imangazaliev/didom
Package didom
Short Description Simple and fast HTML parser
License MIT
Homepage https://github.com/Imangazaliev/DiDOM
Rated 5.00 based on 1 reviews
Informations about the package didom
DiDOM
DiDOM - simple and fast HTML parser.
- README на русском
- DiDOM 1.x documentation. To upgrade from 1.x please checkout the changelog.
Contents
- Installation
- Quick start
- Creating new document
- Search for elements
- Verify if element exists
- Search in element
- Supported selectors
- Changing content
- Output
- Working with elements
- 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
.
Signature:
$string
- an HTML or XML string or a file path.
$isFile
- indicates that the first parameter is a path to a file.
$encoding
- the document encoding.
$type
- the document type (HTML - Document::TYPE_HTML
, XML - Document::TYPE_XML
).
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()
:
Warning: using this method is undesirable because it may be removed in the future.
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 queries.
Search in element
Methods find()
, first()
, xpath()
, has()
, count()
are available in Element too.
Example:
Method findInDocument()
If you change, replace, or remove an element that was found in another element, the document will not be changed. This happens because method find()
of Element
class (a, respectively, the first ()
and xpath
methods) creates a new document to search.
To search for elements in the source document, you must use the methods findInDocument()
and firstInDocument()
:
Warning: methods findInDocument()
and firstInDocument()
work only for elements, which belong to a document, and for elements created via new Element(...)
. If an element does not belong to a document, LogicException
will be thrown;
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
Changing content
Change inner HTML
Change inner XML
Change value (as plain text)
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 owner 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
Waning: you can replace only those elements that were found directly in the document:
More about this in section Search for elements.
Removing element
Warning: you can remove only those elements that were found directly in the document:
More about this in section Search for elements.
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):
Comparison with other parsers
All versions of didom with dependencies
ext-dom Version *
ext-iconv Version *