承接 mathsgod/p-query 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mathsgod/p-query

Composer 安装命令:

composer require mathsgod/p-query

包简介

Using jQuery liked method to parse html by using php

README 文档

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

统计信息

  • 总下载量: 207
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-03-11

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固