This library is an extension of innmind/xml
to support working properly with html as a node tree.
Important: you must use vimeo/psalm
to make sure you use this library correctly.
composer require innmind/html
use Innmind\Html\Reader\Reader;
use Innmind\Xml\Node;
use Innmind\Filesystem\File\Content;
use Innmind\Immutable\Maybe;
$read = Reader::default();
$html = $read(
Content::ofString(\file_get_contents('https://github.com/')),
); // Maybe<Node>
This library provides some visitors to extract elements out of the dom tree, the example below show you how to extract all the h1
elements of a tree:
use Innmind\Html\Visitor\Elements;
$h1s = Elements::of('h1')($html);
Here $h1s
is a set of Element
which are all h1
elements.
Here's the full list of visitors you have access to: