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访问查询mysql数据的三种方法
Oct 09 PHP
PHP里的中文变量说明
Jul 23 PHP
php数组函数序列之array_values() 获取数组元素值的函数与方法
Oct 30 PHP
CI框架装载器Loader.php源码分析
Nov 04 PHP
PHP编译安装时常见错误解决办法
May 28 PHP
简单谈谈PHP vs Node.js
Jul 17 PHP
学习php设计模式 php实现合成模式(composite)
Dec 08 PHP
WordPress中制作导航菜单的PHP核心方法讲解
Dec 11 PHP
php 运算符与表达式详细介绍
Nov 30 PHP
php 如何设置一个严格控制过期时间的session
May 05 PHP
ThinkPHP整合datatables实现服务端分页的示例代码
Feb 10 PHP
php实现断点续传大文件示例代码
Jun 19 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源码之 ext/mysql扩展部分
2009/07/17 PHP
php中__destruct与register_shutdown_function执行的先后顺序问题
2014/10/17 PHP
PHP过滤黑名单关键字的方法
2014/12/01 PHP
Symfony2学习笔记之模板用法详解
2016/03/17 PHP
PHP检测一个数组有没有定义的方法步骤
2019/07/20 PHP
php实现将数组或对象写入到文件的方法小结【三种方法】
2020/04/22 PHP
再谈IE中Flash控件的自动激活 ObjectWrap
2007/03/09 Javascript
延时重复执行函数 lLoopRun.js
2007/05/08 Javascript
CSS常用网站布局实例
2008/04/03 Javascript
jQuery AJAX 调用WebService实现代码
2010/03/24 Javascript
基于jquery的一个简单的脚本验证插件
2010/04/05 Javascript
跟着JQuery API学Jquery 之三 筛选
2010/04/09 Javascript
jquery URL参数判断,确定菜单样式
2010/05/31 Javascript
jQuery控制输入框只能输入数值的小例子
2013/03/20 Javascript
ie8本地图片上传预览示例代码
2014/01/12 Javascript
Javascript中的高阶函数介绍
2015/03/15 Javascript
js判断日期时间有效性的方法
2015/10/24 Javascript
深入理解jquery中的事件与动画
2016/05/24 Javascript
Ionic如何实现下拉刷新与上拉加载功能
2016/06/03 Javascript
Vuejs第十三篇之组件——杂项
2016/09/09 Javascript
详解react-native-fs插件的使用以及遇到的坑
2017/09/12 Javascript
nodejs+mongodb+vue前后台配置ueditor的示例代码
2018/01/02 NodeJs
Node.js爬取豆瓣数据实例分析
2018/03/05 Javascript
JS 5种遍历对象的方式
2020/06/16 Javascript
python赋值操作方法分享
2013/03/23 Python
Python列表(list)、字典(dict)、字符串(string)基本操作小结
2014/11/28 Python
详解python3中zipfile模块用法
2018/06/18 Python
pandas 按照特定顺序输出的实现代码
2018/07/10 Python
mvc框架打造笔记之wsgi协议的优缺点以及接口实现
2018/08/01 Python
浅谈Python中eval的强大与危害
2019/03/13 Python
python实现简单的五子棋游戏
2020/09/01 Python
大学生活动策划方案
2014/02/10 职场文书
庆祝儿童节标语
2014/10/09 职场文书
优秀党员先进事迹材料
2014/12/18 职场文书
PHP实现考试倒计时功能代码
2021/04/16 PHP
java调用Restful接口的三种方法
2021/08/23 Java/Android