gravitypdf/querypath
Composer 安装命令:
composer require gravitypdf/querypath
包简介
PHP library for HTML(5)/XML querying (CSS 4 or XPath) and processing (like jQuery) with PHP 7.1 to 8.5 support
README 文档
README
QueryPath is a jQuery-like library for working with XML and HTML(5) documents in PHP. It is stable software, with the original library garnering 4M+ downloads since first published in 2009.
This is a fork of a fork. The original library, and subsequent fork, are no longer being maintained. The aim of gravitypdf/querypath is to ensure the library remains compatible with the latest version of PHP, and bug free. 🧑💻There is still a lot of legacy code to clean up + modernize, and any assistance given is appreciated.
If you are viewing this file on QueryPath GitHub repository homepage or on Packagist, please note that the default repository branch is
mainwhich can differ from the last stable release.
Installation
composer require gravitypdf/querypath
Basic Usage
To parse HTML or XML:
<?php // Assuming you installed from Composer: require_once __DIR__.'/vendor/autoload.php'; try { // Recommended: uses the masterminds/html5 library to process the HTML $qp = html5qp(__DIR__.'/path/to/file.html'); // load a file from disk $qp = html5qp('<div>You can pass a string of HTML directly to the function</div>'); // load a string } catch (\QueryPath\Exception $e) { // Handle error } try { // Legacy: uses libxml to parse HTML $qp = htmlqp(__DIR__.'/path/to/file.html'); // load a file from disk $qp = htmlqp('<div>You can pass a string of HTML directly to the function</div>'); // load a string } catch (\QueryPath\Exception $e) { // Handle error } try { // XML or XHTML $qp = qp(__DIR__.'/path/to/file.html'); // load a file from disk $qp = qp("<?xml version='1.0'?><hello><world/></hello>"); // load a string } catch (\QueryPath\Exception $e) { // Handle error }
The real power of QueryPath comes from chaining methods together. This example will generate a valid HTML5 document and output to the browser:
try { html5qp(\QueryPath\QueryPath::HTML5_STUB, 'title') // Add some text to the title ->text('Example of QueryPath.') // Now look for the <body> element ->top('body') // Inside the body, add a title and paragraph. ->append('<h1>This is a test page</h1><p>Test text</p>') // Now we select the paragraph we just created inside the body ->children('p') // Add a 'class="some-class"' attribute to the paragraph ->attr('class', 'some-class') // And add a style attribute, too, setting the background color. ->css('background-color', '#eee') // Now go back to the paragraph again ->parent() // Before the paragraph and the title, add an empty table. ->prepend('<table id="my-table"></table>') // Now let's go to the table... ->top('#my-table') // Add a couple of empty rows ->append('<tr></tr><tr></tr>') // select the rows (both at once) ->children() // Add a CSS class to both rows ->addClass('table-row') // Now just get the first row (at position 0) ->eq(0) // Add a table header in the first row ->append('<th>This is the header</th>') // Now go to the next row ->next() // Add some data to this row ->append('<td>This is the data</td>') // Write it all out as HTML ->writeHTML5(); } catch (\QueryPath\Exception $e) { // Handle error }
You can find specific nodes, loop over the matches, and extract information about each element:
try { $html = ' <ul> <li>Foo</li> <li>Bar</li> <li>FooBar</li> </ul>'; $qp = html5qp($html); foreach ($qp->find('li') as $li) { echo $li->text() .'<br>'; } } catch (\QueryPath\Exception $e) { // Handle error }
See the examples directory files for more usages.
Online Manual
The legacy QueryPath manual has been automatically generated from inline DocBlocks using phpDocumentor, and can be found at http://querypath.org.
⚠️ querypath.org is not built or maintained by Gravity PDF, and we have no access to manage or change the website. Help writing new documentation in the repo's Wiki is wanted.
General Troubleshooting
For general questions or troubleshooting please use Discussions.
You can also use the querypath tag at Stack Overflow, as the StackOverflow user base is more likely to answer you in a timely manner.
Contributing
Before submitting issues and pull requests please read CONTRIBUTING.md.
If opening a Pull Request ensure the linter and PHPUnit tests pass (and write a new test for the bug you are fixing):
- Lint:
composer run lint - PHPUnit:
vendor/bin/phpunit
gravitypdf/querypath 适用场景与选型建议
gravitypdf/querypath 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.49M 次下载、GitHub Stars 达 30, 最近一次更新时间为 2022 年 12 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「xml」 「jquery」 「css」 「html」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 gravitypdf/querypath 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 gravitypdf/querypath 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 gravitypdf/querypath 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Yii2 LightBox image galary widget uses Lightbox v2.10.0 by Lokesh Dhakar
Load DOM document safety
This is a laravel 4 package for the server and client side of datatables at http://datatables.net/
A jQuery augmented PHP library for creating and validating HTML forms
Implementation of carouFredSel as extension for Contao Open Source CMS
Added a method to Laravel response for handling xml response and also converting Eloquent return to XML.
统计信息
- 总下载量: 1.49M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 31
- 点击次数: 25
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-12-07