PHPackages                             mathsgod/p-query - 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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. mathsgod/p-query

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

mathsgod/p-query
================

Using jQuery liked method to parse html by using php

3.4.1(1mo ago)02134MITPHPPHP ^8.3CI passing

Since Mar 12Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/mathsgod/p-query)[ Packagist](https://packagist.org/packages/mathsgod/p-query)[ RSS](/packages/mathsgod-p-query/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (10)Dependencies (4)Versions (52)Used By (4)

[![LICENSE](https://camo.githubusercontent.com/d5fc85c6fe6e1e7001761f04d4744c3d0dde43e073981921e92282b9964bad97/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6d61746873676f642f702d7175657279)](https://camo.githubusercontent.com/d5fc85c6fe6e1e7001761f04d4744c3d0dde43e073981921e92282b9964bad97/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6d61746873676f642f702d7175657279)[![PHP Composer](https://github.com/mathsgod/p-query/workflows/PHP%20Composer/badge.svg)](https://github.com/mathsgod/p-query/workflows/PHP%20Composer/badge.svg)[![PHP Version](https://camo.githubusercontent.com/cfbf5f0ec68b44c4f3a2fd881f3498d0df125a4142bb798e4b27d321825a6c8f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e33253246382e34253246382e352d626c7565)](https://camo.githubusercontent.com/cfbf5f0ec68b44c4f3a2fd881f3498d0df125a4142bb798e4b27d321825a6c8f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e33253246382e34253246382e352d626c7565)

Introduction
------------

[](#introduction)

**PQuery** is a PHP library that lets you parse and manipulate HTML strings using a jQuery-like API. It is built on top of PHP's native DOM extension and uses Symfony's CSS Selector component for selector support.

Requirements
------------

[](#requirements)

- PHP ^8.3
- `ext-dom`
- Composer

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

[](#installation)

```
composer require mathsgod/p-query
```

Quick Start
-----------

[](#quick-start)

```
require_once("vendor/autoload.php");

$p = p('
    Hello
');

$p->find(".hello")->text("abc");

echo $p;
/* output

    abc

*/
```

PQuery Supported Methods
------------------------

[](#pquery-supported-methods)

- `size()` / `count()` — number of matched elements
- `first()` / `last()` — get the first or last matched element
- `html()` — get or set inner HTML
- `text()` — get or set text content
- `append()` / `prepend()` — insert content to each element
- `appendTo()` / `prependTo()` — insert elements into target
- `after()` / `before()` — insert content adjacent to elements
- `remove()` — remove elements from the document
- `find()` — search descendants by CSS selector
- `closest()` / `parent()` / `children()` / `contents()` — traverse the DOM
- `attr()` / `removeAttr()` — get or set attributes
- `addClass()` / `removeClass()` / `toggleClass()` / `hasClass()` — class manipulation
- `css()` — get or set inline styles
- `data()` — get or set data attributes
- `val()` — get or set form values
- `filter()` — reduce the matched set
- `each()` — iterate over matched elements
- `replaceWith()` / `wrap()` / `wrapInner()` — replace or wrap elements
- `prev()` / `next()` / `index()` — sibling and position helpers
- `on()` / `trigger()` — event listener helpers

CSS Selector Support
--------------------

[](#css-selector-support)

Use any valid CSS selector with `find()` and `filter()`:

```
$p = p('
    One
    Two
    Three
');

$p->find("li.active")->text("First");
$p->find("#last")->text("Last");
$p->find("li:nth-child(2)")->text("Middle");

echo $p;
```

Working with HTML Elements
--------------------------

[](#working-with-html-elements)

Create and style elements directly:

```
$div = new HTMLDivElement();
$div->classList->add("container");
$div->innerText = "Hello world!";
$div->style->color = "red";

echo $div; // Hello world!
```

### Append an element

[](#append-an-element)

```
$div = new HTMLDivElement();
$p = new HTMLParagraphElement();
$div->append($p);

echo $div; //
```

### Append text

[](#append-text)

```
$div = new HTMLDivElement();
$div->append("Some text");

echo $div; // Some text
```

### Append an element and text

[](#append-an-element-and-text)

```
$div = new HTMLDivElement();
$p = new HTMLParagraphElement();
$div->append("Some text", $p);

echo $div; // Some text
```

Parse HTML from a File
----------------------

[](#parse-html-from-a-file)

```
$p = P\Query::ParseFile("path/to/file.html");
echo $p->find("title")->text();
```

Events
------

[](#events)

Attach and trigger event listeners on elements:

```
$p = p('Click me');
$p->on("click", function (P\Event $e) {
    echo "Clicked!";
});
$p->trigger("click");
```

Running Tests
-------------

[](#running-tests)

```
composer install
composer test
```

Tests are run automatically on GitHub Actions against PHP 8.3, 8.4, and 8.5.

License
-------

[](#license)

MIT

---

Created by Raymond Chong

###  Health Score

56

—

FairBetter than 97% of packages

Maintenance90

Actively maintained with recent releases

Popularity13

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity91

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 99.5% 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 ~64 days

Recently: every ~425 days

Total

48

Last Release

53d ago

Major Versions

1.1.15 → 2.0.02019-03-27

1.x-dev → 2.2.22020-06-19

2.4.0 → 3.0.02021-06-25

PHP version history (7 changes)1.1.0PHP &gt;=5.4

2.0.5PHP &gt;=5.6

2.1.0PHP &gt;=7.1

2.0.13PHP &gt;=7.0

1.2.0PHP &gt;=5.6 &lt;7.0

3.0.0PHP ^8.0

3.4.1PHP ^8.3

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/18732337?v=4)[Raymond](/maintainers/mathsgod)[@mathsgod](https://github.com/mathsgod)

---

Top Contributors

[![mathsgod](https://avatars.githubusercontent.com/u/18732337?v=4)](https://github.com/mathsgod "mathsgod (194 commits)")[![HostLinkUser](https://avatars.githubusercontent.com/u/23697872?v=4)](https://github.com/HostLinkUser "HostLinkUser (1 commits)")

---

Tags

domhtmlphpphp-library

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mathsgod-p-query/health.svg)

```
[![Health](https://phpackages.com/badges/mathsgod-p-query/health.svg)](https://phpackages.com/packages/mathsgod-p-query)
```

###  Alternatives

[craftcms/cms

Craft CMS

3.6k3.7M3.4k](/packages/craftcms-cms)[voku/simple_html_dom

Simple HTML DOM package.

9169.2M88](/packages/voku-simple-html-dom)[sulu/sulu

Core framework that implements the functionality of the Sulu content management system

1.3k1.4M235](/packages/sulu-sulu)[drupal/core-dev

require-dev dependencies from drupal/drupal; use in addition to drupal/core-recommended to run tests from drupal/core.

2023.2M366](/packages/drupal-core-dev)[scotteh/php-dom-wrapper

Simple DOM wrapper to select nodes using either CSS or XPath expressions and manipulate results quickly and easily.

1472.0M14](/packages/scotteh-php-dom-wrapper)[spekulatius/phpscraper

PHPScraper, built with simplicity in mind. See tests/ for more examples.

587158.2k3](/packages/spekulatius-phpscraper)

PHPackages © 2026

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