rdx / js-dom
JS-like DOM traversal
Installs: 1 000
Dependents: 4
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- symfony/css-selector: ^3.1 || ^4.0 || ^5.0 || ^6.0
README
Uses PHP's native DOMDocument
and Symfony's CssSelector
.
use rdx\jsdom\Node;
$doc = Node::create(file_get_contents('pathe.html'));
// Find 1 element. Returns Node|null.
$section = $doc->query('section.schedule-simple');
// Find all elements. Returns array.
$movies = $section->queryAll('.schedule-simple__item');
foreach ($movies as $movie) {
// Every element is a Node.
$a = $movie->query('h4 > a');
// Every element has an innerText/textContent.
$title = $a->getText();
// Attributes are array access.
$href = $a['href'];
}
Known issues:
- Won't select cross-current, like
querySelector[All]
does:
Document:body > section > div
Current:section
Selector:body div
Should match the div, but it doesn't. (jQuery doesn't either.)