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中的字符串函数
Oct 09 PHP
一个php作的文本留言本的例子(三)
Oct 09 PHP
PHP不用第三变量交换2个变量的值的解决方法
Jun 02 PHP
php正则取img标记中任意属性(正则替换去掉或改变图片img标记中的任意属性)
Aug 13 PHP
利用中国天气预报接口实现简单天气预报
Jan 20 PHP
ThinkPHP使用PHPExcel实现Excel数据导入导出完整实例
Jul 22 PHP
php实现转换ubb代码的方法
Jun 18 PHP
weiphp微信公众平台授权设置
Jan 04 PHP
PHP数组去重比较快的实现方式
Jan 19 PHP
php利用云片网实现短信验证码功能的示例代码
Nov 18 PHP
Laravel中unique和exists验证规则的优化详解
Jan 28 PHP
php 下 html5 XHR2 + FormData + File API 上传文件操作实例分析
Feb 28 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接收shell返回的结果中文乱码问题
2014/01/23 PHP
php中实现记住密码下次自动登录的例子
2014/11/06 PHP
PHP的Yii框架中过滤器相关的使用总结
2016/03/29 PHP
PHP查询附近的人及其距离的实现方法
2016/05/11 PHP
thinkPHP5 ACL用户权限模块用法详解
2017/05/10 PHP
php fread函数使用方法总结
2019/05/28 PHP
laravel框架数据库配置及操作数据库示例
2019/10/10 PHP
根据一段代码浅谈Javascript闭包
2010/12/14 Javascript
详谈 Jquery Ajax异步处理Json数据.
2011/09/09 Javascript
把Node.js程序加入服务实现随机启动
2015/06/25 Javascript
JS实现设置ff与ie元素绝对位置的方法
2016/03/08 Javascript
浅析Node.js实现HTTP文件下载
2016/08/05 Javascript
Jquery Easyui进度条组件Progress使用详解(8)
2020/03/26 Javascript
JS+canvas动态绘制饼图的方法示例
2017/09/12 Javascript
QQ跳转支付宝并自动领红包脚本(最新)
2018/06/22 Javascript
NodeJS 实现多语言的示例代码
2018/09/11 NodeJs
读懂CommonJS的模块加载
2019/04/19 Javascript
vue实现codemirror代码编辑器中的SQL代码格式化功能
2019/08/27 Javascript
[53:23]Secret vs Liquid 2018国际邀请赛淘汰赛BO3 第二场 8.25
2018/08/29 DOTA
跟老齐学Python之??碌某?? target=
2014/09/12 Python
Python中处理字符串之isalpha()方法的使用
2015/05/18 Python
Python基于pygame模块播放MP3的方法示例
2017/09/30 Python
人工智能最火编程语言 Python大战Java!
2017/11/13 Python
python测试mysql写入性能完整实例
2018/01/18 Python
Python实现的栈(Stack)
2018/01/26 Python
python通过伪装头部数据抵抗反爬虫的实例
2018/05/07 Python
python各类经纬度转换的实例代码
2019/08/08 Python
python计算导数并绘图的实例
2020/02/29 Python
python matplotlib包图像配色方案分享
2020/03/14 Python
pycharm安装及如何导入numpy
2020/04/03 Python
美国手机支架公司:PopSockets
2019/11/27 全球购物
村委会换届选举方案
2014/05/03 职场文书
社区科普工作方案
2014/06/03 职场文书
施工安全协议书范本
2014/09/26 职场文书
2014年调度员工作总结
2014/11/19 职场文书
python实现求纯色彩图像的边框
2021/04/08 Python