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 jquery 实现新闻标签分类与无刷新分页
Dec 18 PHP
php求两个目录的相对路径示例(php获取相对路径)
Mar 27 PHP
php使用fputcsv()函数csv文件读写数据的方法
Jan 06 PHP
详解PHP中的PDO类
Jul 06 PHP
Zend Framework教程之配置文件application.ini解析
Mar 10 PHP
CI框架入门之MVC简单示例
Nov 21 PHP
thinkPHP5.0框架配置格式、加载解析与读取方法
Mar 17 PHP
php 中htmlentities导致中文无法查询问题
Sep 10 PHP
php使用mysqli和pdo扩展,测试对比连接mysql数据库的效率完整示例
May 09 PHP
Yii2处理密码加密及验证的方法
May 12 PHP
ThinkPHP5&amp;5.1实现验证码的生成、使用及点击刷新功能示例
Feb 07 PHP
如何运行/调试你的PHP代码
Oct 23 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结合js实现点击超链接执行删除确认操作
2014/10/31 PHP
javascript之大字符串的连接的StringBuffer 类
2007/05/08 Javascript
functional继承模式 摘自javascript:the good parts
2011/06/20 Javascript
jquery实现拖拽调整Div大小
2015/01/30 Javascript
Bootstrap基本插件学习笔记之Popover提示框(19)
2016/12/08 Javascript
Vue + Webpack + Vue-loader学习教程之功能介绍篇
2017/03/14 Javascript
JavaScript中一些特殊的字符运算
2017/08/17 Javascript
angular中不同的组件间传值与通信的方法
2017/11/04 Javascript
vue实现点击展开点击收起效果
2018/04/27 Javascript
vue 实现微信浮标效果
2019/09/01 Javascript
Echarts实现多条折线可拖拽效果
2019/12/19 Javascript
利用Python的Django框架中的ORM建立查询API
2015/04/20 Python
Python中处理字符串之endswith()方法的使用简介
2015/05/18 Python
Flask框架Flask-Login用法分析
2018/07/23 Python
pycharm安装和首次使用教程
2018/08/27 Python
python读取文本中的坐标方法
2018/10/14 Python
使用Python OpenCV为CNN增加图像样本的实现
2019/06/10 Python
Python实现一个数组除以一个数的例子
2019/07/20 Python
一行Python代码过滤标点符号等特殊字符
2019/08/12 Python
详细整理python 字符串(str)与列表(list)以及数组(array)之间的转换方法
2019/08/30 Python
基于python实现蓝牙通信代码实例
2019/11/19 Python
如何在python中执行另一个py文件
2020/04/30 Python
Pycharm如何导入python文件及解决报错问题
2020/05/10 Python
python openCV自制绘画板
2020/10/27 Python
python爬虫请求头的使用
2020/12/01 Python
澳大利亚购买最佳炊具品牌网站:Cookware Brands
2019/02/16 全球购物
美国最大的户外装备和服装购物网站:Backcountry
2019/10/15 全球购物
移动通信行业实习自我鉴定
2013/09/28 职场文书
2014年学习厉行节约反对浪费思想汇报
2014/09/10 职场文书
公证委托书格式
2014/09/13 职场文书
2015教师见习期工作总结
2014/12/12 职场文书
新生儿未入户证明
2015/06/23 职场文书
高一语文教学反思
2016/02/16 职场文书
MySQL中几种插入和批量语句实例详解
2021/09/14 MySQL
Centos系统通过Docker安装并搭建MongoDB数据库
2022/04/12 MongoDB
vue css 相对路径导入问题级踩坑记录
2022/06/05 Vue.js