powertools / dom-query
PHP PowerTools DOM-Query 组件
dev-master
2017-10-13 16:24 UTC
Requires
- php: >=5.4.0
- powertools/html5: 1.0
- symfony/css-selector: 2.0
This package is not auto-updated.
Last update: 2024-09-24 07:52:37 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.hasClass
- $selection.insertAfter
- $selection.insertBefore
- $selection.is
- $selection.last
- $selection.parent
- $selection.parents
- $selection.remove
- $selection.removeAttr
- $selection.removeClass
- $selection.text
- $selection.wrap
- 重命名为 'select',显而易见的原因
- 重命名为 'void',因为 'empty' 是 PHP 中的保留词