noi/querypath-format

QueryPath 扩展,增加格式化节点值的额外方法

v1.0 2013-10-25 03:43 UTC

This package is not auto-updated.

Last update: 2024-09-24 00:29:46 UTC


README

FormatExtension 是一个 QueryPath 扩展,增加了以下方法

  1. format($callback [, $args, [, $... ]])
  2. formatAttr($name, $callback [, $args, [, $... ]])

安装

使用 Composer,运行

$ php composer.phar require noi/querypath-format "*"

或者,您可以手动编辑您的 composer.json 并添加以下内容

{
    "require": {
        "noi/querypath-format": "*"
    }
}

使用方法

format()

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

快速示例

<?php
require_once '/path/to/vendor/autoload.php';
QueryPath::enable('Noi\QueryPath\FormatExtension');
$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();

输出

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

formatAttr()

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

快速示例

<?php
require_once '/path/to/vendor/autoload.php';
QueryPath::enable('Noi\QueryPath\FormatExtension');
$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();

输出

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

许可证

FormatExtension 采用 MIT 许可证授权 - 详细信息请参阅 LICENSE 文件。