mathsgod / p-query
Using jQuery liked method to parse html by using php
3.4.1
2026-06-25 06:46 UTC
Requires
- php: ^8.3
- symfony/css-selector: ^7.2
Requires (Dev)
Suggests
None
Provides
None
Conflicts
None
Replaces
None
- dev-main
- 3.4.1
- 3.4.0
- 3.3.0
- 3.2.2
- 3.2.1
- 3.2.0
- 3.1.1
- 3.1.0
- 3.0.2
- 3.0.1
- 3.0.0
- 2.4.0
- 2.3.0
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.16
- 2.0.15
- 2.0.14
- 2.0.13
- 2.0.12
- 2.0.11
- 2.0.10
- 2.0.9
- 2.0.8
- 2.0.7
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.x-dev
- 1.2.0
- 1.1.15
- 1.1.14
- 1.1.13
- 1.1.12
- 1.1.11
- 1.1.10
- 1.1.2
- 1.1.1
- 1.1.0
This package is auto-updated.
Last update: 2026-08-25 07:48:10 UTC
README
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
- PHP ^8.3
ext-dom- Composer
Installation
composer require mathsgod/p-query
Quick Start
require_once("vendor/autoload.php"); $p = p('<div class="container"> <div class="hello">Hello</div> </div>'); $p->find(".hello")->text("abc"); echo $p; /* output <div class="container"> <div class="hello">abc</div> </div> */
PQuery Supported Methods
size()/count()— number of matched elementsfirst()/last()— get the first or last matched elementhtml()— get or set inner HTMLtext()— get or set text contentappend()/prepend()— insert content to each elementappendTo()/prependTo()— insert elements into targetafter()/before()— insert content adjacent to elementsremove()— remove elements from the documentfind()— search descendants by CSS selectorclosest()/parent()/children()/contents()— traverse the DOMattr()/removeAttr()— get or set attributesaddClass()/removeClass()/toggleClass()/hasClass()— class manipulationcss()— get or set inline stylesdata()— get or set data attributesval()— get or set form valuesfilter()— reduce the matched seteach()— iterate over matched elementsreplaceWith()/wrap()/wrapInner()— replace or wrap elementsprev()/next()/index()— sibling and position helperson()/trigger()— event listener helpers
CSS Selector Support
Use any valid CSS selector with find() and filter():
$p = p('<ul> <li class="active">One</li> <li>Two</li> <li id="last">Three</li> </ul>'); $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
Create and style elements directly:
$div = new HTMLDivElement(); $div->classList->add("container"); $div->innerText = "Hello world!"; $div->style->color = "red"; echo $div; // <div class="container" style="color: red">Hello world!</div>
Append an element
$div = new HTMLDivElement(); $p = new HTMLParagraphElement(); $div->append($p); echo $div; // <div><p></p></div>
Append text
$div = new HTMLDivElement(); $div->append("Some text"); echo $div; // <div>Some text</div>
Append an element and text
$div = new HTMLDivElement(); $p = new HTMLParagraphElement(); $div->append("Some text", $p); echo $div; // <div>Some text<p></p></div>
Parse HTML from a File
$p = P\Query::ParseFile("path/to/file.html"); echo $p->find("title")->text();
Events
Attach and trigger event listeners on elements:
$p = p('<button>Click me</button>'); $p->on("click", function (P\Event $e) { echo "Clicked!"; }); $p->trigger("click");
Running Tests
composer install
composer test
Tests are run automatically on GitHub Actions against PHP 8.3, 8.4, and 8.5.
License
MIT
Created by Raymond Chong