定制 arthurkushman/query-path 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

arthurkushman/query-path

Composer 安装命令:

composer require arthurkushman/query-path

包简介

HTML/XML querying (CSS 4 or XPath) and processing (like jQuery)

README 文档

README

Scrutinizer Code Quality Build Status Code Intelligence Status codecov License: MIT

At A Glance

QueryPath is a jQuery-like library for working with XML and HTML documents in PHP. It now contains support for HTML5 via the HTML5-PHP project.

Why this lib was forked and recoded

  • Legacy code (repo was left for > 3 years) didn't allow to support new features of PHP>=7.1
  • A lot of DeaDBeaF code like: unused params, unused local variables etc
  • A lot of needless flow structures
  • DRY/KISS/SOLID rules were thrown away when it was developed
  • Minor bugs and fragile functionality

Installation

composer require arthurkushman/query-path 

Gettings Started

Assuming you have successfully installed QueryPath via Composer, you can parse documents like this:

// HTML5 (new)
$qp = html5qp("path/to/file.html");

// Legacy HTML via libxml
$qp = htmlqp("path/to/file.html");

// XML or XHTML
$qp = qp("path/to/file.html");

// All of the above can take string markup instead of a file name:
$qp = qp("<?xml version='1.0'?><hello><world/></hello>")

But the real power comes from chaining. Check out the example below.

Example Usage

Say we have a document like this:

<?xml version="1.0"?>
<table>
  <tr id="row1">
    <td>one</td><td>two</td><td>three</td>
  </tr>
  <tr id="row2">
    <td>four</td><td>five</td><td>six</td>
  </tr>
</table>

And say that the above is stored in the variable $xml. Now we can use QueryPath like this:

<?php
// Add the attribute "foo=bar" to every "td" element.
qp($xml, 'td')->attr('foo', 'bar');

// Print the contents of the third TD in the second row:
echo qp($xml, '#row2>td:nth(3)')->text();

// Append another row to the XML and then write the
// result to standard output:
qp($xml, 'tr:last')->after('<tr><td/><td/><td/></tr>')->writeXML();

?>

(This example is in examples/at-a-glance.php.)

With over 60 functions and robust support for chaining, you can accomplish sophisticated XML and HTML processing using QueryPath.

From there, the main functions you will want to use are qp() (alias of QueryPath::with()) and htmlqp() (alias of QueryPath::withHTML()).

QueryPath Format Extension

format()

\QueryPath\DOMQuery format(callable $callback [, mixed $args [, $... ]])

A quick example:

<?php
QueryPath::enable(Format::class);
$qp = qp('<?xml version="1.0"?><root><div>_apple_</div><div>_orange_</div></root>');

$qp->find('div')
        ->format('strtoupper')
        ->format('trim', '_')
        ->format(function ($text) {
            return '*' . $text . '*';
        });

$qp->writeXML();

OUTPUT:

<?xml version="1.0"?>
<root>
  <div>*APPLE*</div>
  <div>*ORANGE*</div>
</root>

formatAttr()

\QueryPath\DOMQuery formatAttr(string $name, callable $callback [, mixed $args [, $... ]])

A quick example:

<?php
QueryPath::enable(Format::class);
$qp = qp('<?xml version="1.0"?><root>' .
        '<item label="_apple_" total="12,345,678" />' .
        '<item label="_orange_" total="987,654,321" />' .
        '</root>');

$qp->find('item')
        ->formatAttr('label', 'trim', '_')
        ->formatAttr('total', 'str_replace[2]', ',', '');

$qp->find('item')->formatAttr('label', function ($value) {
    return ucfirst(strtolower($value));
});

$qp->writeXML();

OUTPUT:

<?xml version="1.0"?>
<root>
  <item label="Apple" total="12345678"/>
  <item label="Orange" total="987654321"/>
</root>

arthurkushman/query-path 适用场景与选型建议

arthurkushman/query-path 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.24M 次下载、GitHub Stars 达 3, 最近一次更新时间为 2018 年 07 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「xml」 「jquery」 「css」 「html」 「xslt」 「PHP7」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 arthurkushman/query-path 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 arthurkushman/query-path 我们能提供哪些服务?
定制开发 / 二次开发

基于 arthurkushman/query-path 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.24M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 33
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 2
  • Forks: 114
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-07-25