Skip to content
Thomas Weinert edited this page Jul 8, 2015 · 6 revisions

Optionally you can use CSS selectors with FluentDOM\Query and FluentDOM\Document/FluentDOM\Element (FluentDOM >= 5.3 only).

FluentDOM <= 5.2 can use Carica/PhpCss or Symfony/CssSelector. With version 5.3 an interface and connector packages were added. The connectors use a library to parse the CSS selector and convert it into an Xpath expression.

Connector packages:

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 (>= 5.3)

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

$document = new FluentDOM\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