QueryPath(QP)库 在 PHP 中实现了类似于 jQuery 的效果,用它还可以方便地处理 XML HTML...功能太强大了!!!
A QueryPath Tutorial(一个简易说明)
QueryPath makes use of method chaining to provide a concise suite of tools for manipulating a DOM.
The basic principle of method chaining is that each method returns an object upon which additional methods can be called. In our case, the QueryPath object usually returns itself.
Let's take a look at an example to illustrate:
$qp = qp(QueryPath::HTML_STUB); // Generate a new QueryPath object.(创建一个 QP 对象)
$qp2 = $qp->find('body'); // Find the body tag.(找到 "body" 标签)
// Now the surprising part:(请看下面让你惊奇的地方)
if ($qp === $qp2) {
// This will always get printed.(它总是会这样输出)
print "MATCH";
}
Why does $qp always equal $qp2? Because the find() function does all of its data gathering and then returns the QueryPath object.
This might seem esoteric, but it all has a very practical rationale. With this sort of interface, we can chain lots of methods together:
(你可以向使用 jQuery 一样来连缀方法)
qp(QueryPath::HTML_STUB)->find('body')->text('Hello World')->writeHTML();
In this example, we have four method calls:
qp(QueryPath::HTML_STUB): Create a new QueryPath object and provide it with a stub of an HTML document. This returns the QueryPath object.
find('body'): This searches the QueryPath document looking for an element named 'body'. That element is, of course, the
复制代码 代码如下:
Most of that HTML comes from the QueryPath::HTML_STUB. All we did was add the Hello World text inside of the
tags.复制代码 代码如下: Test text
/**
* Using QueryPath.
*
* This file contains an example of how QueryPath can be used
* to generate web pages.
* @package QueryPath
* @subpackage Examples
* @author M Butcher
* @license LGPL The GNU Lesser GPL (LGPL) or an MIT-like license.
*/
// Require the QueryPath core.
require_once 'QueryPath/QueryPath.php';
// Begin with an HTML stub document (XHTML, actually), and navigate to the title.
qp(QueryPath::HTML_STUB, 'title')
// Add some text to the title
->text('Example of QueryPath.')
// Now look for the element
->find(':root body')
// Inside the body, add a title and paragraph.
->append('This is a test page
// Now we select the paragraph we just created inside the body
->children('p')
// Add a 'class="some-class"' attribute to the paragraph
->attr('class', 'some-class')
// And add a style attribute, too, setting the background color.
->css('background-color', '#eee')
// Now go back to the paragraph again
->parent()
// Before the paragraph and the title, add an empty table.
->prepend('')
// Now let's go to the table...
->find('#my-table')
// Add a couple of empty rows
->append('')
// select the rows (both at once)
->children()
// Add a CSS class to both rows
->addClass('table-row')
// Now just get the first row (at position 0)
->eq(0)
// Add a table header in the first row
->append('This is the header ')
// Now go to the next row
->next()
// Add some data to this row
->append('This is the data ')
// Write it all out as HTML
->writeHTML();
?>
The code above produces the following HTML:
复制代码 代码如下:
This is the header |
---|
This is the data |
Test text
Now you should have an idea of how QueryPath works. Grab a copy of the library and try it out! Along with the source code, you will get a nice bundle of HTML files that cover every single public function in the QueryPath library (no kidding). There are more examples there, too.
不错的东东!赶紧 Grab 它吧~~!
感谢您的阅读,更多相关文章请关注PHP中文网(www.php.cn)!