{"id":17489257,"url":"https://github.com/berliozframework/htmlselector","last_synced_at":"2025-06-16T23:02:02.834Z","repository":{"id":37549716,"uuid":"109764134","full_name":"BerliozFramework/HtmlSelector","owner":"BerliozFramework","description":"PHP library to do queries on HTML files (converted in SimpleXMLElement object) like jQuery on DOM.","archived":false,"fork":false,"pushed_at":"2024-10-18T11:53:28.000Z","size":149,"stargazers_count":7,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"2.x","last_synced_at":"2025-06-16T23:01:46.057Z","etag":null,"topics":["composer","css-pseudo","html","php","php-library"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/BerliozFramework.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-11-06T23:58:36.000Z","updated_at":"2025-01-26T09:30:11.000Z","dependencies_parsed_at":"2024-11-14T13:13:47.733Z","dependency_job_id":"9e363d77-74a5-423c-b10c-113e92f68050","html_url":"https://github.com/BerliozFramework/HtmlSelector","commit_stats":{"total_commits":64,"total_committers":2,"mean_commits":32.0,"dds":0.0625,"last_synced_commit":"f8804521dd8f7804e9776009d662adc91d636815"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/BerliozFramework/HtmlSelector","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BerliozFramework%2FHtmlSelector","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BerliozFramework%2FHtmlSelector/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BerliozFramework%2FHtmlSelector/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BerliozFramework%2FHtmlSelector/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/BerliozFramework","download_url":"https://codeload.github.com/BerliozFramework/HtmlSelector/tar.gz/refs/heads/2.x","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BerliozFramework%2FHtmlSelector/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260256240,"owners_count":22981805,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["composer","css-pseudo","html","php","php-library"],"created_at":"2024-10-19T05:06:41.113Z","updated_at":"2025-06-16T23:02:02.214Z","avatar_url":"https://github.com/BerliozFramework.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Berlioz HTML Selector\n\n[![Latest Version](https://img.shields.io/packagist/v/berlioz/html-selector.svg?style=flat-square)](https://github.com/BerliozFramework/HtmlSelector/releases)\n[![Software license](https://img.shields.io/github/license/BerliozFramework/HtmlSelector.svg?style=flat-square)](https://github.com/BerliozFramework/HtmlSelector/blob/2.x/LICENSE)\n[![Build Status](https://img.shields.io/github/actions/workflow/status/BerliozFramework/HtmlSelector/tests.yml?branch=2.x\u0026style=flat-square)](https://github.com/BerliozFramework/HtmlSelector/actions/workflows/tests.yml?query=branch%3A2.x)\n[![Quality Grade](https://img.shields.io/codacy/grade/d234908cbf01419387c3c1cb9098be7e/2.x.svg?style=flat-square)](https://www.codacy.com/manual/BerliozFramework/HtmlSelector)\n[![Total Downloads](https://img.shields.io/packagist/dt/berlioz/html-selector.svg?style=flat-square)](https://packagist.org/packages/berlioz/html-selector)\n\n**Berlioz HTML Selector** is a PHP library to do queries on HTML files with CSS selectors like *jQuery* on DOM.\n\n## Installation\n\n### Composer\n\nYou can install **Berlioz HTML Selector** with [Composer](https://getcomposer.org/), it's the recommended installation.\n\n```bash\n$ composer require berlioz/html-selector\n```\n\n### Dependencies\n\n- **PHP** ^8.0\n- PHP libraries:\n    - **dom**\n    - **libxml**\n    - **mbstring**\n    - **simplexml**\n\n## Usage\n\n### Load HTML\n\nYou can easily load an HTML string or file with the static function `HtmlSelector::query()`. For files, use second\nparameter `contentsIsFile` of method.\n\n```php\n$htmlSelector = new \\Berlioz\\HtmlSelector\\HtmlSelector();\n\n$query = $htmlSelector-\u003equery('\u003chtml\u003e\u003cbody\u003e...\u003c/body\u003e\u003c/html\u003e');\n$query = $htmlSelector-\u003equery('path-of-my-file/file.html', contentsIsFile: true);\n$query = $htmlSelector-\u003equery(new SimpleXMLElement(/*...*/));\n```\n\n### Load from `ResponseInterface`\n\n`HtmlSelector::queryFromResponse()` permit loading html of a response body.\n\n```php\n$htmlSelector = new \\Berlioz\\HtmlSelector\\HtmlSelector();\n\n/** @var \\Psr\\Http\\Message\\ResponseInterface $response */\n$query = $htmlSelector-\u003equeryFromResponse($response);\n```\n\n### Do a query\n\nIt's very simple to query an HTML string with a selector like *jQuery*.\n\n```php\n/** @var \\Berlioz\\HtmlSelector\\Query\\Query $query */\n$query = $query-\u003efind('body \u003e .wrapper h2');\n$query = $query-\u003efilter(':first');\n```\n\n## Selectors\n\n### CSS Simple selectors\n\n- **type**: selection of elements with their type.\n- **#id**: selection of an element with it's ID.\n- **.class**: selection of elements with their class.\n- Attributes selections.\n    - **[attribute]**: with attribute 'attribute'.\n    - **[attribute=foo]**: value of attribute equals to 'foo'.\n    - **[attribute^=foo]**: value of attribute starts with 'foo'.\n    - **[attribute$=foo]**: value of attribute ends with 'foo'.\n    - **[attribute*=foo]**: value of attribute contains 'foo'.\n    - **[attribute!=foo]**: value of attribute different of 'foo'.\n    - **[attribute~=foo]**: value of attribute contains word 'foo'.\n    - **[attribute|=foo]**: value of attribute contains prefix 'foo'.\n\n### CSS Ascendants, descendants, multiples\n\n- ***selector* *selector*** or ***selector* \u003e\u003e *selector***: all descendant selector.\n- ***selector* \u003e *selector***: direct descendant selector (only children).\n- ***selector* ~ *selector***: siblings selector.\n- ***selector*, *selector***: multiple selectors.\n\n### CSS Pseudo Classes\n\n- **:any(selector, selector)**: only elements given in arguments.\n- **:any-link**: only elements of type `\u003ca\u003e`, `\u003carea\u003e` and `\u003clink\u003e`, with `[href]` attribute.\n- **:blank**: only elements without child, and no text (except spaces).\n- **:checked**: only elements with attribute `[checked]`.\n- **:dir**: only elements with directional text given (default: ltr).\n- **:disabled**: only elements of type `\u003cbutton\u003e`, `\u003cinput\u003e`, `\u003coptgroup\u003e`, `\u003cselect\u003e` or `\u003ctextarea\u003e` with `[disabled]`\n  attribute.\n- **:empty**: only elements without child.\n- **:enabled**: only elements of type `\u003cbutton\u003e`, `\u003cinput\u003e`, `\u003coptgroup\u003e`, `\u003coption\u003e`, `\u003cselect\u003e`, `\u003ctextarea\u003e`\n  , `\u003cmenuitem\u003e` or `\u003cfieldset\u003e` without `[disabled]` attribute.\n- **:first**: only first result of complete selection.\n- **:first-child**: only firsts children in their parents.\n- **:first-of-type**: only firsts type in their parents.\n- **:has(selector, selector)**: only elements who valid child selector.\n- **:lang(x)**: only elements with attribute `[lang]` prefixed by or equals to given value.\n- **:last-child**: only lasts in their parents.\n- **:last-of-type**: only lasts type in their parents.\n- **:not(selector, selector)**: filter 'not'.\n- **:nth-child()**: *n* elements in selector result.\n- **:nth-last-child()**: *n* elements in selector result, start at end of list.\n- **:nth-of-type()**: *n* elements of given type in selector result.\n- **:nth-last-of-type()**: *n* elements of given type in selector result, start at end of list.\n- **:only-child**: only elements who are only child in the parent.\n- **:only-of-type**: only elements who are only type child in the parent.\n- **:optional()**: only input elements without `[required]` attribute.\n- **:read-only()**: only elements that the user cannot edit.\n- **:read-write()**: only elements with editable property.\n- **:required()**: only elements with `[required]` attribute.\n- **:root()**: get root element.\n\n### Additional CSS Pseudo Classes (not in CSS specifications) from jQuery library\n\n- **:button**: only elements of type `\u003cbutton\u003e` without attribute value `[type=submit]` or `\u003cinput type=\"button\"\u003e`.\n- **:checkbox**: only elements with attribute `[type=checkbox]`.\n- **:contains(x)**: only elements who contain text given.\n- **:eq(x)**: only result with index given (index start to 0).\n- **:even**: only even results in selection.\n- **:file**: only elements with attribute `[type=file]`.\n- **:gt(x)**: only result with an index greater than index given (index start to 0).\n- **:gte**: only result with an index greater than or equal to index given (index start to 0).\n- **:header**: only elements of heading, like `\u003ch1\u003e`, `\u003ch2\u003e`...\n- **:image**: only elements with attribute `[type=image]`.\n- **:input**: only elements of type `\u003cinput\u003e`, `\u003ctextarea\u003e`, `\u003cselect\u003e` or `\u003cbutton\u003e`.\n- **:last**: only last result of complete selection.\n- **:lt**: only result with index leather than index given (index start to 0).\n- **:lte**: only result with index leather than or equal to index given (index start to 0).\n- **:odd**: only odd results in selection.\n- **:parent**: only elements with one child or more.\n- **:password**: only elements with attribute `[type=password]`.\n- **:radio**: only elements with attribute `[type=radio]`.\n- **:reset**: only elements with attribute `[type=reset]`.\n- **:selected**: only elements of type `\u003coption\u003e` with attribute `[selected]`.\n- **:submit**: only elements of type `\u003cbutton\u003e` or `\u003cinput\u003e` with attribute `[type=submit]`.\n- **:text**: only elements of type `\u003cinput\u003e` with attribute `[type=text]` or without `[type]` attribute.\n\n### Additional CSS Pseudo Classes (not in CSS specifications)\n\n- **:count(x)**: only elements who are x children in the parent, used in **:has(selector)** pseudo class.\n\n### Full example of selectors\n\n```\nselect \u003e option:selected\ndiv#myId.class1.class2[name1=value1][name2=value2]:even:first\n```\n\n## Functions\n\n### Default functions\n\nSome default functions are available in Query object to interact with results. The functions should have the same result\nas their counterparts on jQuery.\n\n- **attr(name)**: get attribute value\n- **attr(name, value)**: set attribute value\n- **children()**: get children of elements in result.\n- **count()**: count the number of elements in query result.\n- **data(nameOfData)**: get data value (name is with camelCase syntax without the 'data-' prefix).\n- **filter(selector)**: filter elements in result.\n- **find(selector)**: find selector in elements in result.\n- **get(i)**: get DOM element in result.\n- **hasClass(class_name)**: know if least one of element in result have given classes.\n- **html()**: get html of first element in result.\n- **index(selector)**: get the index of given selector in result elements.\n- **is(selector)**: know if selector valid the least one element in result.\n- **isset(i)**: return boolean to know if an element key exists in result.\n- **next(selector)**: get next element after each element in result.\n- **nextAll(selector)**: get all next elements after each element in result.\n- **not(selector)**: filter elements in result.\n- **parent()**: get direct parent of current result of selecting.\n- **parents(selector)**: get all parents of current result of selecting.\n- **prev(selector)**: get prev element after each element in result.\n- **prevAll(selector)**: get all prev elements after each element in result.\n- **prop(name)**: get property boolean value of an attribute, used for example for `disabled` attribute.\n- **prop(name, value)**: set property boolean value of an attribute, used for example for `disabled` attribute.\n- **serialize()**: serialize input values of a form. Return a string.\n- **serializeArray()**: serialize input values of a form. Return an array.\n- **text()**: get text of each element concatenated.\n- **val()**: get value of a form element.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fberliozframework%2Fhtmlselector","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fberliozframework%2Fhtmlselector","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fberliozframework%2Fhtmlselector/lists"}