-
Notifications
You must be signed in to change notification settings - Fork 20
Read HTML
FluentDOM extends the DOM classes. Like SimpleXML, FluentDOM implements a lot of PHP magic functions and interfaces. Unlike SimpleXML, it does not hide the DOM API. It just provides a lot of nice shortcuts. Additionally FluentDOM provides a static class as a starting point.
As a first example let's load some HTML and extract all link URLs and captions.
$htmlFile = 'sample.html';
$links = [];
$document = FluentDOM::load(
$htmlFile,
'text/html',
[FluentDOM\Loader\Options::ALLOW_FILE => TRUE]
);
foreach ($document('//a[@href]') as $a) {
$links[] = [
'caption' => (string)$a,
'href' => $a['href']
];
}
var_dump($links);
The static function FluentDOM::load()
loads a given source and returns an FluentDOM\DOM\Document
. It can load different formats (depending on the installed plugins). As a security measure the standard loaders will not load files without an option - only strings.
FluentDOM\DOM\Document
and the other extended node classes implement the magic method __invoke()
. So they can be used like functions. This will execute a Xpath expression with the node as the context.
The extended node classes can be cast to string. This will return all text context of the node. It includes the text content of descendant nodes.
FluentDOM\DOM\Element
(element nodes) implement ArrayAccess
. So you can use array syntax. A string offset will access the attribute value, an integer offset a child node.
- Home
- Getting Started
- Tasks
- Plugins
- Functions
- Lists
- Creator (5.1)
- CSS Selectors
- Convertors
- Loaders
- Serializers (5.1)
- Transformers (5.1)
- Extended DOM
- XMLReader (6.1)
- XMLWriter (6.1)
- Interfaces