querypath/querypath

HTML/XML查询和处理(类似于jQuery)

3.0.5 2016-08-01 22:40 UTC

README

QueryPath 现由 GravityPDF 的优秀团队维护更新。 https://github.com/GravityPDF/querypath。本仓库中的 QueryPath 版本不再维护。建议您使用 GravityPDF 的版本。 -- Matt, 2022年12月

QueryPath: 找到你的路径。

Stability: Maintenance

作者:Matt Butcher(主要),Emily Brand 和许多人

网站 | API 文档 | VCS 和问题跟踪 | 支持列表 | 开发者列表 | Pear 频道 |

本软件包采用 MIT 许可证(COPYING-MIT.txt)授权。

概览

QueryPath 是一个类似 jQuery 的库,用于在 PHP 中处理 XML 和 HTML 文档。它现在包含通过 HTML5-PHP 项目 对 HTML5 的支持。

入门

假设您已通过 Composer 成功安装了 QueryPath,您可以这样解析文档

require_once "vendor/autoload.php";

// 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>")

但真正的力量来自于链式调用。请看下面的示例。

示例用法

假设我们有一个这样的文档

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

假设上面存储在变量 $xml 中。现在我们可以这样使用 QueryPath

<?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:
print 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();

?>

(此示例在 examples/at-a-glance.php 中。)

拥有超过 60 个函数和强大的链式调用支持,您可以使用 QueryPath 完成复杂的 XML 和 HTML 处理。

QueryPath 安装器

安装 QueryPath 的首选方法是使用 Composer

您也可以从 GitHub 下载此软件包。

Composer(首选)

要将 QueryPath 添加为项目中的库,请将以下内容添加到 composer.json 文件的 'require' 部分

{
  "require": {
    "querypath/QueryPath": ">=3.0.0"
  }
}

在该目录中运行 php composer.phar install

要跟踪稳定代码,您可以使用 dev-master 而不是 >=3.0.0

手动安装

您可以从 GitHub 标签页面 下载稳定版本,或者您可以使用 git 克隆 此存储库 并从代码中工作。

包含 QueryPath

从 QueryPath 3.x 版本开始,如果使用 Composer 安装,QueryPath 使用 Composer 自动加载器

<?php
require 'vendor/autoload.php';
?>

没有 Composer,您可以像这样包含 QueryPath

<?php
require 'QueryPath/src/qp.php';
?>

QueryPath 也可以编译成 Phar,然后像这样包含

<?php
require 'QueryPath.phar';
?>

从那里开始,您将想要使用的主要函数是 qp()QueryPath::with() 的别名)和 htmlqp()QueryPath::withHTML() 的别名)。从 API 文档 开始。