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超级全局变量
Jan 26 PHP
php获取post中的json数据的实现方法
Jun 08 PHP
php数组函数序列之in_array() 查找数组值是否存在
Oct 29 PHP
phpmailer中文乱码问题的解决方法
Apr 22 PHP
php使用ZipArchive提示Fatal error: Class ZipArchive not found in的解决方法
Nov 04 PHP
PHP中变量引用与变量销毁机制分析
Nov 15 PHP
php字符串截取函数用法分析
Nov 25 PHP
PHP实现动态柱状图改进版
Mar 30 PHP
PHP记录搜索引擎蜘蛛访问网站足迹的方法
Apr 15 PHP
php快速排序原理与实现方法分析
May 26 PHP
Thinkphp5 微信公众号token验证不成功的原因及解决方法
Nov 12 PHP
浅谈laravel5.5 belongsToMany自身的正确用法
Oct 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
PHP4与PHP3中一个不兼容问题的解决方法
2006/10/09 PHP
用php实现选择排序的解决方法
2013/05/04 PHP
getJSON跨域SyntaxError问题分析
2014/08/07 PHP
php读取csv文件并输出的方法
2015/03/14 PHP
php编写简单的文章发布程序
2015/06/18 PHP
php中mkdir()函数的权限问题分析
2016/09/24 PHP
PHP消息队列实现及应用详解【队列处理订单系统和配送系统】
2019/05/20 PHP
一个可以显示阴历的JS代码
2007/03/05 Javascript
JS 页面内容搜索,类似于 Ctrl+F功能的实现代码
2007/08/13 Javascript
为javascript添加String.Format方法
2020/08/11 Javascript
JS实现的生成随机数的4个函数分享
2015/02/11 Javascript
一次$.getJSON不执行的简单记录
2016/07/19 Javascript
Bootstrap栅格系统学习笔记
2016/11/25 Javascript
jquery实现文字单行横移或翻转(上下、左右跳转)
2017/01/08 Javascript
Vue.js结合Ueditor富文本编辑器的实例代码
2017/07/11 Javascript
vue项目在安卓低版本机显示空白的原因分析(两种)
2018/09/04 Javascript
Vue项目History模式404问题解决方法
2018/10/31 Javascript
elementui实现预览图片组件二次封装
2020/12/29 Javascript
javascript实现倒计时提示框
2021/03/02 Javascript
[54:28]EG vs OG 2019国际邀请赛小组赛 BO2 第一场 8.16
2019/08/18 DOTA
教你用一行Python代码实现并行任务(附代码)
2018/02/02 Python
Python 装饰器实现DRY(不重复代码)原则
2018/03/05 Python
python如何通过twisted实现数据库异步插入
2018/03/20 Python
python中update的基本使用方法详解
2019/07/17 Python
Python Django模板之模板过滤器与自定义模板过滤器示例
2019/10/18 Python
python使用正则表达式去除中文文本多余空格,保留英文之间空格方法详解
2020/02/11 Python
python GUI库图形界面开发之PyQt5中QMainWindow, QWidget以及QDialog的区别和选择
2020/02/26 Python
利用python查看数组中的所有元素是否相同
2021/01/08 Python
python如何实现递归转非递归
2021/02/25 Python
canvas绘制文本内容自动换行的实现代码
2019/01/14 HTML / CSS
PatPat德国:妈妈的每日优惠
2019/10/02 全球购物
城管大队整治方案
2014/05/06 职场文书
党的群众路线剖析材料
2014/10/09 职场文书
2019秋季运动会口号
2019/06/25 职场文书
JavaWeb 入门篇(3)ServletContext 详解 具体应用
2021/07/16 Java/Android
Netty分布式客户端接入流程初始化源码分析
2022/03/25 Java/Android