Skip to content

Repository files navigation

QueryPath FormatExtension

FormatExtension is a QueryPath extension that adds the following methods:

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

Installation

With Composer, run:

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

Alternatively, you can edit your composer.json manually and add the following:

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

Usage

format()

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

A quick example:

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

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:

<?phprequire_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) {
returnucfirst(strtolower($value));
});
$qp->writeXML();

OUTPUT:

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

License

FormatExtension is licensed under the MIT License - see the LICENSE file for details.

About

A QueryPath extension that adds extra methods for formatting node values.

Resources

Stars

3 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages