--- breadcrumb: - Components - HTML Selector keywords: - html - css - selector - jquery - dom - query --- # HTML Selector > ℹ️ **Note**: The HTML selector is part of the **Berlioz** ecosystem but is available as a standalone package: > [`berlioz/html-selector`](https://github.com/BerliozFramework/HtmlSelector). > You can find it on [Packagist](https://packagist.org/packages/berlioz/html-selector). > You can use it independently of the framework, in any PHP application. **Berlioz HTML Selector** is a PHP library to do queries on HTML files with CSS selectors like *jQuery* on DOM. ## Load HTML You can easily load an HTML string or file with the static function `HtmlSelector::query()`. For files, use second parameter `contentsIsFile` of method. ```php $htmlSelector = new \Berlioz\HtmlSelector\HtmlSelector(); $query = $htmlSelector->query('
...'); $query = $htmlSelector->query('path-of-my-file/file.html', contentsIsFile: true); $query = $htmlSelector->query(new SimpleXMLElement(/*...*/)); ``` ## Load from `ResponseInterface` `HtmlSelector::queryFromResponse()` permit loading html of a response body. ```php $htmlSelector = new \Berlioz\HtmlSelector\HtmlSelector(); /** @var \Psr\Http\Message\ResponseInterface $response */ $query = $htmlSelector->queryFromResponse($response); ``` ## Do a query It's very simple to query an HTML string with a selector like *jQuery*. ```php /** @var \Berlioz\HtmlSelector\Query\Query $query */ $query = $query->find('body > .wrapper h2'); $query = $query->filter(':first'); ``` ## Selectors ### CSS Simple selectors - **type**: selection of elements with their type. - **#id**: selection of an element with it's ID. - **.class**: selection of elements with their class. - Attributes selections. - **[attribute]**: with attribute 'attribute'. - **[attribute=foo]**: value of attribute equals to 'foo'. - **[attribute^=foo]**: value of attribute starts with 'foo'. - **[attribute$=foo]**: value of attribute ends with 'foo'. - **[attribute*=foo]**: value of attribute contains 'foo'. - **[attribute!=foo]**: value of attribute different of 'foo'. - **[attribute~=foo]**: value of attribute contains word 'foo'. - **[attribute|=foo]**: value of attribute contains prefix 'foo'. ### CSS Ascendants, descendants, multiples - ***selector* *selector*** or ***selector* >> *selector***: all descendant selector. - ***selector* > *selector***: direct descendant selector (only children). - ***selector* ~ *selector***: siblings selector. - ***selector*, *selector***: multiple selectors. ### CSS Pseudo Classes - **:any(selector, selector)**: only elements given in arguments. - **:any-link**: only elements of type ``, `` and ``, with `[href]` attribute. - **:blank**: only elements without child, and no text (except spaces). - **:checked**: only elements with attribute `[checked]`. - **:dir**: only elements with directional text given (default: ltr). - **:disabled**: only elements of type `