CSS Selector in PHP
Learn how to create a new Inphinit project.
Supported selectores
{placeholder}
use Inphinit\DOM\Document;
$handle = new Document(Document::HTML);
$handle->load(...
);
$elements = $handle->selector()->all('body > div');
var_dump($elements);
{placeholder}
$element = $handle->selector()->first('#foo');
var_dump($element);
Supported selectores
Selector | Description | Note |
---|---|---|
* |
any element (universal selector) | - |
E |
an element of type E |
- |
E.warning |
an E element whose class is warning (class selector) |
- |
E#myid |
an E element with ID equal to myid (ID selector) |
- |
E F |
an F element descendant of an E element (descendant combinator) |
- |
E > F |
an F element child of an E element (child combinator) |
- |
E + F |
an F element immediately preceded by an E element |
- |
E ~ F |
an F element preceded by an E element |
- |
E[foo] |
an E element with a foo attribute |
- |
E[foo="bar"] |
an E element whose foo attribute value is exactly equal to bar |
- |
E[foo="bar" i] |
To match attribute values case-insensitively, the attribute selector may include the identifier i before the closing bracket ] |
also supported by ^= , $= , *= and |= |
E[foo~="bar"] |
an E element whose foo attribute value is a list of whitespace-separated values, one of which is exactly equal to bar |
- |
E[foo^="bar"] |
an E element whose foo attribute value begins exactly with the string bar |
- |
E[foo$="bar"] |
an E element whose foo attribute value ends exactly with the string bar |
- |
E[foo*="bar"] |
an E element whose foo attribute value contains the substring bar |
- |
E[foo|="en"] |
an E element whose foo attribute has a hyphen-separated list of values beginning (from the left) with "en" |
- |
E:empty |
an E element that has no children (including text nodes) |
- |
E:lang(fr) |
an element of type E in language fr |
- |
E:first-child |
an E element, first child of its parent |
- |
E:last-child |
an E element, last child of its parent |
- |
E:nth-child(n) |
an E element, the n-th child of its parent |
only a few combinations are supported, like:
:nth-child(<a>n+<b>) ,
:nth-child(<a>n) ,
:nth-child(<a>) ,
:nth-child(even) ,
:nth-child(odd) (<a> and <b> represents numbers in form)
|
E:contains(bar) |
an E element whose contents contains the substring bar |
non-standard |
E:contains-child(bar) |
an E element whose contents contains the substring bar only on nodeTexts that are direct children |
non-standard |