QueryPath PHP 中的jQuery


Posted in PHP onApril 11, 2010

官方主页  http://querypath.org/

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 <body></body> portion of the HTML document. When it finds the body element, it keeps an internal pointer to that element, and it returns the QueryPath object (which is now wrapping the body element).
text('Hello World'): This function takes the current element(s) wrapped by QueryPath and adds the text Hello World. As you have probably guessed, it, too, returns a QueryPath object. The object will still be pointing to the body element.
writeHTML(): The writeHTML() function prints out the entire document. This is used to send the HTML back to the client. You'll never guess what this function returns. Okay, you guessed it. QueryPath.
So at the end of the chain above, we would have created a document that looks something like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta> 
<title>Untitled</title> 
</head> 
<body>Hello World</body> 
</html>

Most of that HTML comes from the QueryPath::HTML_STUB. All we did was add the Hello World text inside of the <body></body> tags.
Not all QueryPath functions return QueryPath objects. Some tools need to return other data. But those functions are well-documented in the included documentation.
These are the basic principles behind QueryPath. Now let's take a look at a larger example that exercises more of the QueryPath API.
A Longer Example
This example illustrates various core features of QueryPath.
In this example, we use some of the standard QueryPath functions (most of them implementing the jQuery interface) to build a new web page from scratch.
Each line of the code has been commented individually. The output from this is shown in a separate block beneath.
<?php 
/** 
* Using QueryPath. 
* 
* This file contains an example of how QueryPath can be used 
* to generate web pages. 
* @package QueryPath 
* @subpackage Examples 
* @author M Butcher <matt@aleph-null.tv> 
* @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 <body> element 
->find(':root body') 
// Inside the body, add a title and paragraph. 
->append('<h1>This is a test page</h1><p>Test text</p>') 
// 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('<table id="my-table"></table>') 
// Now let's go to the table... 
->find('#my-table') 
// Add a couple of empty rows 
->append('<tr></tr><tr></tr>') 
// 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('<th>This is the header</th>') 
// Now go to the next row 
->next() 
// Add some data to this row 
->append('<td>This is the data</td>') 
// Write it all out as HTML 
->writeHTML(); 
?>

The code above produces the following HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta> 
<title>Example of QueryPath.</title> 
</head> 
<body> 
<table id="my-table"> 
<tr class="table-row"><th>This is the header</th></tr> 
<tr class="table-row"><td>This is the data</td></tr> 
</table> 
<h1>This is a test page</h1> 
<p class="some-class" style="background-color: #eee">Test text</p></body> 
</html>

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 相关文章推荐
一个自定义位数的php多用户计数器代码
Mar 11 PHP
php中实现简单的ACL 完结篇
Sep 07 PHP
PHP计数器的实现代码
Jun 08 PHP
PHP 安全检测代码片段(分享)
Jul 05 PHP
php+mysql不用递归实现的无限级分类实例(非递归)
Jul 08 PHP
PHP 导出Excel示例分享
Aug 18 PHP
PHP获取当前相对于域名目录的方法
Jun 26 PHP
apache php mysql开发环境安装教程
Jul 28 PHP
Zend Framework入门应用实例详解
Dec 11 PHP
php中get_magic_quotes_gpc()函数说明
Feb 06 PHP
[原创]PHP正则匹配中英文、数字及下划线的方法【用户名验证】
Aug 01 PHP
php识别翻转iphone拍摄的颠倒图片
May 17 PHP
10个可以简化php开发过程的MySQL工具
Apr 11 #PHP
Fatal error: Call to undefined function curl_init()解决方法
Apr 09 #PHP
PHP Socket 编程
Apr 09 #PHP
有关JSON以及JSON在PHP中的应用
Apr 09 #PHP
dedecms系统的广告设置代码 基础版本
Apr 09 #PHP
PHP 动态随机生成验证码类代码
Apr 09 #PHP
DedeCMS 核心类TypeLink.class.php摘要笔记
Apr 07 #PHP
You might like
解析在PHP中使用全局变量的几种方法
2013/06/24 PHP
php中filter_input函数用法分析
2014/11/15 PHP
PHP中实现接收多个name相同但Value不相同表单数据实例
2015/02/03 PHP
PHP输出两个数字中间有多少个回文数的方法
2015/03/23 PHP
laravel 解决groupBy时出现的错误 isn't in Group By问题
2019/10/17 PHP
JavaScript 反科里化 this [译]
2012/09/20 Javascript
JavaScript实现x秒后自动跳转到一个页面
2013/01/03 Javascript
JS判断对象是否存在的10种方法总结
2013/12/23 Javascript
javascript实现在某个元素上阻止鼠标右键事件的方法和实例
2014/08/12 Javascript
javascript动态控制服务器控件实例
2014/09/05 Javascript
使用Meteor配合Node.js编写实时聊天应用的范例
2015/06/23 Javascript
JavaScript 模块的循环加载实现方法
2015/12/13 Javascript
静态页面html中跳转传值的JS处理技巧
2016/06/22 Javascript
nodejs制作爬虫实现批量下载图片
2017/05/19 NodeJs
基于JavaScript实现数码时钟效果
2020/03/30 Javascript
vue组件命名和props命名代码详解
2019/09/01 Javascript
[01:00:26]Ti4主赛事胜者组第一天 EG vs NEWBEE 1
2014/07/19 DOTA
[14:21]VICI vs EG (BO3)
2018/06/07 DOTA
python生成日历实例解析
2014/08/21 Python
python使用wxpython开发简单记事本的方法
2015/05/20 Python
Python编程中归并排序算法的实现步骤详解
2016/05/04 Python
全面了解Python的getattr(),setattr(),delattr(),hasattr()
2016/06/14 Python
使用anaconda的pip安装第三方python包的操作步骤
2018/06/11 Python
华为校园招聘上机笔试题 扑克牌大小(python)
2020/04/22 Python
libreoffice python 操作word及excel文档的方法
2019/07/04 Python
Python Numpy计算各类距离的方法
2019/07/05 Python
Python字符串格式化输出代码实例
2019/11/22 Python
基于python 等频分箱qcut问题的解决
2020/03/03 Python
美国潜水装备、水肺潜水和浮潜设备商店:Leisure Pro
2018/08/08 全球购物
Tostadora意大利:定制T恤
2019/04/08 全球购物
美国气象仪器、花园装饰和墙壁艺术商店:Wind & Weather
2019/05/29 全球购物
德国领先的大尺码和超大尺码男装在线零售商:Bigtex
2019/06/22 全球购物
环境保护建议书
2014/08/26 职场文书
资源环境与城乡规划管理专业自荐书
2014/09/26 职场文书
学生检讨书怎么写
2015/05/07 职场文书
Python tensorflow卷积神经Inception V3网络结构
2022/05/06 Python