Download the PHP package zenthangplus/html-dom-parser without Composer
On this page you can find all versions of the php package zenthangplus/html-dom-parser. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download zenthangplus/html-dom-parser
More information about zenthangplus/html-dom-parser
Files in zenthangplus/html-dom-parser
Package html-dom-parser
Short Description A Simple HTML DOM parser written in PHP let you manipulate HTML in a easy way with CSS Selector.
License MIT
Informations about the package html-dom-parser
HTML DOM parser for PHP
A Simple HTML DOM parser written in PHP let you manipulate HTML in a easy way with selectors just like CSS or jQuery.
This is modern version of Simple HTML DOM. You can install by using Composer and import to your project as a package.
Features
- Parse and modify HTML document.
- Find tags (elements) on HTML with selectors just like jQuery.
- Extract contents from HTML in a single line.
- Export elements or a special node to a single file.
- Supports HTML document with invalid structure.
Installation
You can use Composer to install this package to your project by running following command:
The minimum PHP version requirement is 5.6. If you are using PHP < 5.6, please use the original version.
Usage
The following example is the simple usage of this package:
DOM
Dom is the root Node of the HTML document.
You can load DOM from string
or file
.
NODE
Node is simply an HTML element that described as an object.
You can also load any Node (similar to Dom):
Traversing the DOM
By using selectors like jQuery or CSS, you can traverse easy in the Node.
Example:
Similar to Dom, a Node also traversable:
List of supported selectors:
Selector example | Description |
---|---|
div |
Find elements with the div tag |
#container |
Find elements with the container id |
.wrapper |
Find elements with the wrapper class |
[data-id] |
Find elements with the data-id attribute |
[data-id=12] |
Find elements with the attribute data-id=12 |
a[data-id=12] |
Find anchor tags with the attribute data-id=12 |
*[class] |
Find all elements with class attribute |
a, img |
Find all anchors and images |
a[title], img[title] |
Find all anchors and images with the title attribute |
#container ul |
By using space between selectors, you can find nested elements |
#container>ul |
By using > between selectors, you can find the closest children |
#container, #side |
By using , between selectors, you can find elements by multiple selectors in one query |
#container div.content ul>li, #side div[role=main] ul li |
You can combine selectors in one query |
List of function you can use with above selectors:
find()
Find elementsfindOne()
Find one element
Specific find functions:
getElementById()
Get a element by IDgetElementByTagName()
Get a element by tag namegetElementsByTagName()
Get elements by tag name
Accessing the node's data:
text()
Get the text contentsgetAttributes()
Get attributesgetAttribute()
Get a attributehasAttribute()
Check element has a attributehasChild()
Check element has childinnerHtml()
Get inner HTMLouterHtml()
Get outer HTMLinnerXml()
Get inner XML- Get node's HTML
Modifying the Node's data
setAttribute()
Set a attributeremoveAttribute()
Remove a attributeappendChild()
Append childsave()
Save DOM or even a node
Traversing the Node tree
getChild()
Get child elementgetChildren()
Get all childrengetFirstChild()
Get first childgetLastChild()
Get last childgetNextSibling()
Get next siblinggetPrevSibling()
Get previous siblingfindAncestorTag()
Find ancestor tag