Skip to content
Thomas Weinert edited this page Jul 12, 2018 · 6 revisions

FluentDOM uses an interface and connector package were to add CSS selector support. The connectors use a library to parse the CSS selector and convert it into an Xpath expression.

Connector packages:

After you install them you can use CSS selectors.

FluentDOM::QueryCss()

This static function of the FluentDOM class returns an instance of FluentDOM\Query. It allows to use CSS selectors as arguments that are expected to be an selector string.

Example:

$result = \FluentDOM::QueryCss($html, 'text/html')
  ->find('p')
  ->find('span')
  ->filter('.mark')
  ->addClass('red');

Selectors API

FluentDOM\DOM\Document, FluentDOM\DOM\Element, FluentDOM\DOM\DocumentFragment support the Selectors API Level 1. You can use querySelector() and querySelectorAll().

$document = new FluentDOM\DOM\Document();
$document->loadHTML($html);

foreach ($document->querySelectorAll('li') as $li) {
  var_dump((string)$li);
}

var_dump(
  $document->querySelector('ul li:nth-child(2)')->textContent
);
Clone this wiki locally