PHPackages                             manychois/cici - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. manychois/cici

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

manychois/cici
==============

A PHP library which lets you use CSS selector to locate elements in an HTML document.

0.1.3(1y ago)2551[6 PRs](https://github.com/manychois/cici/pulls)1MITPHPPHP &gt;=8.2CI passing

Since Oct 29Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/manychois/cici)[ Packagist](https://packagist.org/packages/manychois/cici)[ RSS](/packages/manychois-cici/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (10)Versions (12)Used By (1)

Cici
====

[](#cici)

[![PHP Version Require](https://camo.githubusercontent.com/39396309c31f5fdc826d15a33a2b734983540277f7a3e591a3433fb73af0aa65/68747470733a2f2f706f7365722e707567782e6f72672f6d616e7963686f69732f636963692f726571756972652f706870)](https://packagist.org/packages/manychois/cici) [![Latest Stable Version](https://camo.githubusercontent.com/16c07ef95017479af9f20a1d472102db63b182848a1821a051bad04784cfd3ad/68747470733a2f2f706f7365722e707567782e6f72672f6d616e7963686f69732f636963692f76)](https://packagist.org/packages/manychois/cici) [![License](https://camo.githubusercontent.com/4194ba58aeef90a87b74aaa898d2c3d0a96750728ae15910e0305a61ca64ca86/68747470733a2f2f706f7365722e707567782e6f72672f6d616e7963686f69732f636963692f6c6963656e7365)](https://packagist.org/packages/manychois/cici) [![codecov](https://camo.githubusercontent.com/c6ecbba697e280f46a16301d74a5653de725d93d1a0a31e19c22557dbe6bff30/68747470733a2f2f636f6465636f762e696f2f6769746875622f6d616e7963686f69732f636963692f67726170682f62616467652e7376673f746f6b656e3d3078584b7654376f7871)](https://codecov.io/github/manychois/cici)

Cici is a PHP library which lets you use CSS selector to locate elements in an HTML document.

Features
--------

[](#features)

- Wide support of CSS3 selector syntax, e.g. `.home dl > dd:nth-child(3n+1 of :has([href$="s.php"]))`.
- Support for namespace checking in element names and attribute names, e.g. `svg|circle`.
- Complete CSS selector parser written in PHP.
- No dependency on external libraries.
- Write your own DOM library with CSS selector support by implementing `Manychois\Cici\Matching\AbstractMatchContext`.

Installation
------------

[](#installation)

```
composer require manychois/cici
```

Usage
-----

[](#usage)

The class `Manychois\Cici\DomQuery` provides three methods to match element(s) by the given CSS selectors:

- `closest(\DOMElement $element, string $selector, array $nsLookup = []): ?\DOMElement`
    Traverses the element and its parents (heading toward the document root) until it finds an element that matches the specified CSS selector.
- `query(\DOMNode $scope, string $selector, array $nsLookup = []): ?\DOMElement`
    Traverses the descendants of the node (in document order) until it finds an element that matches the specified CSS selector.
- `queryAll(\DOMNode $scope, string $selector, array $nsLookup = []): \Generator`
    Traverses the descendants of the node (in document order) and finds all elements that match the specified CSS selector.

They all accept an optional argument `$nsLookup` which is an associative array of namespace prefixes and URIs. This is useful when you need to match elements with specific namespace URI. If you need to define a default namespace, use the empty string `''` as the key.

Examples
--------

[](#examples)

### Locate an element

[](#locate-an-element)

```
$doc = new \DOMDocument();
$doc->loadHTML('Hello world!');
$query = new \Manychois\Cici\DomQuery();
$element = $query->query($doc, '#div-1');
echo $element->textContent . \PHP_EOL;
// Output: Hello world!
```

### Locate an element with a namespace URI

[](#locate-an-element-with-a-namespace-uri)

```
$xml =  'http://www.w3.org/2000/svg']);
echo $element->getAttribute('r') . PHP_EOL;
// Output: 40
```

### Locate multiple elements

[](#locate-multiple-elements)

```
$html = textContent . PHP_EOL;
}
// Output:
// Item 2
// Item 4
```

Supported CSS selectors
-----------------------

[](#supported-css-selectors)

SelectorExampleNotesAttribute selector`[attr="value" i]`Namespace prefix is supported.
Case-sensitivity modifier is supported.Class selector`.primary`ID selector`#main`Type selector`div`Namespace prefix is supported.Universal selector`*`Namespace prefix is supported.Pseudo-class selector`:nth-child(2n+1 of .selected)`Check the section below for supported pseudo-classes.Compound selector`div.active`Child combinator`ul > li`Descendant combinator`nav a`Next-sibling combinator`p + img`Subsequent-sibling combinator`div ~ img`Selector list`ul, ol`### Supported pseudo-classes

[](#supported-pseudo-classes)

List in alphabetical order:

- `:any-link`
- `:checked`
- `:disabled`
- `:empty`
- `:enabled`
- `:first-child`
- `:first-of-type`
- `:has`
- `:indeterminate`
- `:is`
- `:last-child`
- `:last-of-type`
- `:not`
- `:nth-child`
- `:nth-last-child`
- `:nth-last-of-type`
- `:nth-of-type`
- `:only-child`
- `:only-of-type`
- `:optional`
- `:read-only`
- `:read-write`
- `:required`
- `:root`
- `:scope`
- `:where`

Unsupported CSS selectors
-------------------------

[](#unsupported-css-selectors)

- All pseudo-elements, e.g. `::before`.
- Column combinator, i.e. `||`.
- All pseudo-classes which are not listed in the previous section. Basically anything which involves user interaction, e.g. `:hover`, will never be supported.

Performance concern
-------------------

[](#performance-concern)

This library parses the CSS selector string, builds a selector tree, and then traverses the DOM tree in depth-first search order to locate the element(s). Since everything is written in PHP, this allows a wider support of CSS selector syntax. However, this also means that the performance is not comparable to the native PHP functions, e.g. `Document->getElementById()`, `DOMXPath->query()`.

In a basic performance test, this library can be 2x to 10x slower than equilvalent `DOMXPath->query()`. As long as you are fine with the performance of a native PHP foreach loop, this library should be good enough for you. You can run `composer run benchmark` to see the performance comparison.

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance62

Regular maintenance activity

Popularity13

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 76% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~5 days

Total

4

Last Release

545d ago

PHP version history (2 changes)0.1.0PHP &gt;=8.3

0.1.1PHP &gt;=8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/e1ba7e0a3d6113e5704a03007e1d40c208af6ac5ec3a57e389ac426c661cbbb1?d=identicon)[manychois](/maintainers/manychois)

---

Top Contributors

[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (19 commits)")[![manychois](https://avatars.githubusercontent.com/u/3290769?v=4)](https://github.com/manychois "manychois (6 commits)")

---

Tags

cssdomhtmlphpselectorcsshtmldomselector

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/manychois-cici/health.svg)

```
[![Health](https://phpackages.com/badges/manychois-cici/health.svg)](https://phpackages.com/packages/manychois-cici)
```

###  Alternatives

[wa72/htmlpagedom

jQuery-inspired DOM manipulation extension for Symfony's Crawler

3383.9M34](/packages/wa72-htmlpagedom)[rct567/dom-query

DomQuery is a PHP library that allows easy 'jQuery like' DOM traversing and manipulation

134261.0k4](/packages/rct567-dom-query)[phpstrap/phpstrap

Bootstrap layout generator

1214.7k](/packages/phpstrap-phpstrap)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
