mathsgod/p-query
Composer 安装命令:
composer require mathsgod/p-query
包简介
Using jQuery liked method to parse html by using php
README 文档
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
统计信息
- 总下载量: 207
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-03-11