GithubHelp home page GithubHelp logo

ruslan14 / htmldomparser Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zenthangplus/htmldomparser

0.0 0.0 0.0 117 KB

A Simple HTML DOM parser written in PHP let you manipulate HTML in a easy way with selectors just like CSS or jQuery.

Shell 0.64% PHP 98.80% HTML 0.21% Dockerfile 0.35%

htmldomparser's Introduction

HTML DOM parser for PHP

Travis Status CodeShip Status CircleCI

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:

composer require zenthangplus/html-dom-parser

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:

<?php
$dom = \HTMLDomParser\DomFactory::load('<div class="container"><div class="anchor"><a href="#">Test</a></div></div>');
$a = $dom->findOne('.container a');
echo $a->text();
// Output: Test

DOM

Dom is the root Node of the HTML document.

You can load DOM from string or file.

<?php
$dom = \HTMLDomParser\DomFactory::load('<div>Test</div>');
<?php
$dom = \HTMLDomParser\DomFactory::loadFile('document.html');

NODE

Node is simply an HTML element that described as an object.

You can also load any Node (similar to Dom):

<?php
$node = \HTMLDomParser\NodeFactory::load('<div><a href="#">Test</a></div>');
<?php
$node = \HTMLDomParser\NodeFactory::loadFile('document.html');

Traversing the DOM

By using selectors like jQuery or CSS, you can traverse easy in the Dom or even in a Node.

Example:

<?php
$dom = \HTMLDomParser\DomFactory::loadFile('document.html');
$dom->find('div');
$dom->find('#container');
$dom->find('#container .content ul>li a.external-link');
$dom->find('#container .content ul>li a[data-id=link-1]');

Similar to Dom, a Node also traversable:

<?php
$dom = \HTMLDomParser\DomFactory::loadFile('document.html');
$node = $dom->findOne('#container .content ul>li');
$anchorNode = $node->findOne('a.external-link');

// Even traverse in a separate Node
$node = \HTMLDomParser\NodeFactory::load('<ul class="list"><li>Item 1</li><li>Item 2</li></ul>');
$node->find('ul.list li');
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:
Specific find functions:
Accessing the node's data:
Modifying the Node's data
Traversing the Node tree

htmldomparser's People

Contributors

zenthangplus avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.