rkaiser0324 / dom-query
PHP PowerTools DOM-Query 组件
dev-master
2019-09-18 19:49 UTC
Requires
- php: >=5.4.0
This package is auto-updated.
Last update: 2024-09-20 00:13:22 UTC
README
PHPPowertools 是一个PHP >= 5.4的Web应用程序框架。
PHPPowertools/DOM-Query 是PHPPowertools的第一个公开发布的组件。
该组件的目的是提供一个类似于jQuery的界面来爬取XML和HTML文档。在底层,它使用 symfony/CssSelector 将 CSS选择器 转换为 XPath查询
jQuery的方式
// 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'));
DOM-Query的方式
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'));
示例使用
// 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('<div class="site-center"></div>'); // 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><div class="footer-wrapper"></div></section>'); [...]
支持的方法
- $ (1)
- $.parseHTML
- $.parseXML
- $.parseJSON
- $selection.add
- $selection.addClass
- $selection.after
- $selection.append
- $selection.attr
- $selection.before
- $selection.children
- $selection.closest
- $selection.contents
- $selection.detach
- $selection.each
- $selection.eq
- $selection.empty (2)
- $selection.find
- $selection.first
- $selection.get
- $selection.insertAfter
- $selection.insertBefore
- $selection.last
- $selection.parent
- $selection.parents
- $selection.remove
- $selection.removeAttr
- $selection.removeClass
- $selection.text
- $selection.wrap
- 为了显而易见的原因重命名了'select'。
- 由于'empty'在PHP中是保留字,因此重命名为'void'。