2017 © Pedro Peláez
 

library dom-query

PHP PowerTools DOM-Query Component

image

rkaiser0324/dom-query

PHP PowerTools DOM-Query Component

  • Tuesday, May 8, 2018
  • by kschipulring
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 9 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

DOM Query Component

PHPPowertools is a web application framework for PHP >= 5.4., (*1)

PHPPowertools/DOM-Query is the first component of the PHPPowertools that has been released to the public., (*2)

The purpose of this component is to provide a jQuery-like interface for crawling XML and HTML documents. Under the hood, it uses symfony/CssSelector for converting CSS selectors to XPath queries, (*3)


The jQuery way :
// Find the elements that match selector 'div.foo'
$s = $('div.foo');

// Pass an element object (DOM Element)
$s = $(document.body);

// Pass a jQuery object
$s = $($('p + p'));
The DOM-Query way :
namespace App;
use \PowerTools\DOM_Query;

// Get file content
$htmlcode = file_get_contents('https://github.com');

// Create a new DOM_Query instance, using a string as a source
$H = new DOM_Query($htmlcode);

// Create a new DOM_Query instance, using an existing DOM_Query instance as a source
$H = new DOM_Query($H->select('body'));

// Find the elements that match selector 'div.foo'
$s = $H->select('div.foo');

// Pass an element object (DOM Element)
$s = $H->select($documentBody);

// Pass a DOM Query instance
$s = $H->select($H->select('p + p'));

Example use :
// Select the body tag
$body = $H->select('body');

// Combine different classes as one selector to get all site blocks
$siteblocks = $body->select('.site-header, .masthead, .site-body, .site-footer');

// Nest your methods just like you would with jQuery
$siteblocks->select('button')->add('span')->addClass('icon icon-printer');

// Use a lambda function to set the text of all site blocks
$siteblocks->text(function($i, $val) {
    return $i . " - " . $val->attr('class');
});

// Append the following HTML to all site blocks
$siteblocks->append('

'); // Use a descendant selector to select the site's footer $sitefooter = $body->select('.site-footer > .site-center'); // Set some attributes for the site's footer $sitefooter->attr(array('id' => 'aweeesome', 'data-val' => 'see')); // Use a lambda function to set the attributes of all site blocks $siteblocks->attr('data-val', function($i, $val) { return $i . " - " . $val->attr('class') . " - photo by Kelly Clark"; }); // Select the parent of the site's footer $sitefooterparent = $sitefooter->parent(); // Remove the class of all i-tags within the site's footer's parent $sitefooterparent->select('i')->removeAttr('class'); // Wrap the site's footer within two nex selectors $sitefooter->wrap('<section> </section>'); [...]

Supported methods :

  1. Renamed 'select', for obvious reasons
  2. Renamed 'void', since 'empty' is a reserved word in PHP

Author
twitter/johnslegers
John slegers

The Versions

08/05 2018

dev-simplified

dev-simplified https://github.com/kschipulring/DOM-Query

PHP PowerTools DOM-Query Component

  Sources   Download

MIT

The Requires

  • php >=5.4.0 || ^7.0

 

by John Slegers
by Karl Schipul

01/05 2018

dev-master

9999999-dev https://github.com/PHPPowertools/DOM-Query

PHP PowerTools DOM-Query Component

  Sources   Download

MIT

The Requires

 

by John Slegers
by Karl Schipul

01/05 2018

v1.0.1

1.0.1.0 https://github.com/PHPPowertools/DOM-Query

PHP PowerTools DOM-Query Component

  Sources   Download

MIT

The Requires

 

by John Slegers
by Karl Schipul

30/04 2018

1.0

1.0.0.0 https://github.com/PHPPowertools/DOM-Query

PHP PowerTools DOM-Query Component

  Sources   Download

MIT

The Requires

 

by John Slegers