mathsgod/p-query

Using jQuery liked method to parse html by using php

Maintainers

Package info

github.com/mathsgod/p-query

pkg:composer/mathsgod/p-query

Statistics

Installs: 207

Dependents: 4

Suggesters: 0

Stars: 0

Open Issues: 0

3.4.1 2026-06-25 06:46 UTC

README

LICENSE PHP Composer PHP Version

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 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

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