2017 © Pedro Peláez
 

library htmlparser

HTML parser and DOM implementation

image

gaswelder/htmlparser

HTML parser and DOM implementation

  • Thursday, July 5, 2018
  • by gaswelder
  • Repository
  • 1 Watchers
  • 0 Stars
  • 10 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 11 % Grown

The README.md

HTML parser

This is an HTML parser with a minimal DOM implementation. It doesn't depend on PHP's bundled libxml and DOM and handles some of the broken markup encountered in the wild., (*1)

Usage example

<?php
use \gaswelder\htmlparser\Parser;
use \gaswelder\htmlparser\ParsingException;

try {
    $doc = Parser::parse($html);
} catch(ParsingException $e) {
    // ...
    return;
}

$images = $doc->querySelectorAll('#posts .post img');
foreach ($images as $img) {
    $src = $img->getAttribute('src');
    echo $src, "\n";
}

Features

All container nodes (DocumentNode and ElementNode) have the querySelector and querySelectorAll methods which support a limited subset of CSS2:, (*2)

  • type selectors (like div)
  • some attribute selectors ([checked], [attr="val"], [attr$="val"], [attr^="val"])
  • class selectors (.active)
  • ID selectors (#main)

Also they support these combinators:, (*3)

  • descendant (ul li)
  • child (ul > li)
  • sibling (li + li)

The nodes can be printed to the console, and the output will be similar to Firefox's console:, (*4)

<?php
$list = $doc->getElementsByTagName('a');
echo $list, PHP_EOL;

might produce this output:, (*5)

NodeList [ <a>, <a#top>, <a.first>, <a>, <a> ]

Installation

Composer dudes do this in the console:, (*6)

composer require gaswelder/htmlparser

Old-school dudes (if still alive) may download the library to whatever \$libdir they have and do this:, (*7)

require "$libdir/htmlparser/init.php";

The Versions

05/07 2018

dev-master

9999999-dev

HTML parser and DOM implementation

  Sources   Download

MIT

The Development Requires

02/07 2017

1.1.1

1.1.1.0

HTML parser and DOM implementation

  Sources   Download

MIT

The Development Requires

01/07 2017

1.1.0

1.1.0.0

HTML parser and DOM implementation

  Sources   Download

MIT

The Development Requires

15/11 2016

1.0

1.0.0.0

HTML parser and DOM implementation

  Sources   Download

MIT