QueryPath PHP 中的jQuery_php技巧

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

QP API 手册  http://api.querypath.org/docs/

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 它吧~~!

时间: 2024-07-31 11:46:26

QueryPath PHP 中的jQuery_php技巧的相关文章

轻松快捷 Photoshop中的快捷键技巧详解

技巧|详解 Photoshop中的快捷键技巧虽已是老生常谈,但是网上通常都是简单讲述,本文将从应用功能出发详细讲解这些常用的快捷键的使用.相信对大家都会有帮助. ■ 你可以用以下的快捷键来快速浏览你的图像: Home卷动至图像的左上角:End卷动至图像的右下角:Page UP卷动至图像的上方:Page Down卷动至图像的下方:Ctrl+Page Up卷动至图像的左方:Ctrl+Page Down卷动至图像的右方. ■ 按Ctrl键+Alt键+0键即可使图像按1:1比例显示. ■ 当你想"紧排&

网页设计中常用的技巧:最常见的固定浮窗设计

文章描述:固定浮窗在设计中的使用. 固定浮窗是网页设计中常用的技巧,指一定区块固定在浏览器的响应位置而不随滚动条的移动而改变位置的设计. 最常见的固定浮窗设计是固定在浏览器底部的弹窗广告,比如新浪首页会有新浪视窗的弹窗广告. 最近当当网首页广告力度很大,其中大部分都是固定在浏览器底部和两边. 由于固定浮窗总是显示在网页内容的最上端,有时候甚至覆盖了用户想要的价值内容,这种设计确实不讨人喜欢.有时候,有的固定浮窗会提供"关闭"按钮,但是这明显不能消除用户的反感情绪,因为在一片广告上找关闭

用户体验设计:浅谈可用性测试中沟通的技巧

文章描述:如何快速解除用户防备?--浅谈可用性测试中沟通的技巧.   一般来说,在产品的设计和开发过程中,不同阶段会使用到不同的用户研究方法.比如,在产品正式发布之前,通常会进行可用性测试.可用性测试,是指让一群有代表性的用户尝试对产品进行典型操作,同时观察员和开发人员在一旁观察.聆听.记录.该产品可能是一个网站.软件,或其他任何产品,它可能已经做好,也可能尚未成型. 对于一个典型的可用行测试,我们可以:1. 通过观察用户在使用产品过程中出现的一些问题,发现产品的可用性问题2. 从测试参与者的表

在word2007中选择文本技巧大全

在word2007文档中在对文本进行操作的时候,大多数都需先选择要操作文本,才能进行相应的操作.选择文本的时候,我们通常使用按住鼠标拖动的方法选择文本.下面再列举一些选择文本的技巧 ★选择文档中的一句话:按住Ctrl键,并在这句话任意地方单击鼠标左键,即可选中该句话.不过需要注意的是,这里说的一句话指的是句号.分号.冒号.感叹号等两者之间的文字,但不包括逗号.引号.顿号等 ★选择文档中的多句话:按住Shift键不放,并在你要选择的起始处单击鼠标左键,然后在你要选择的结尾处单击鼠标左键. ★选择文

在excel表格中回车操作技巧介绍

  在excel表格中回车操作技巧介绍          ● 多个单元格快速填充同一内容 选中多个单元格,输入内容后按Ctrl加回车,这些单元格就填上了相同的内容. ● 单元格中回车不仅能向下跳转 在表格某个单元格中按下回车后,光标会自动跑到下一个单元格.那么能不能做到按回车后光标跑到上面.前面.后面的单元格呢? 其实,通过"文件→选项",在"高级"设置窗口中,就能看到控制光标移动方向的机关了. ● 单元格内输入多行内容咋换行 想在一个单元格内输入多行内容吗?输入完

在PPT2010中快速抠图技巧

如果手头没有photoshop美图秀秀等图片处理工具,怎样快速抠图呢?在这里小编打算来教大家如何使用PPT2010来抠图. PPT教程 在PPT2010中快速抠图技巧 1.以下面幻灯片中的图片为例,我想扣掉图片中黑色外框部分,但要保留月亮中黑色的人体剪影.首先,鼠标单击选中图片,切换到[图片工具]下的[格式]选项卡,在[调整]功能组中单击[删除背景]按钮. 2.演示文稿切换到[背景消除]选项卡.如图紫色区域是要被删除的部分.我们先拖动选框四角,将月亮部分框选在保留区域内. 3.单击[标记要保留的

Excel 2007表格中字段设置技巧

  Excel是我们的工作生活中经常使用到的一款办公软件,掌握了Excel表格中字段设置技巧可以大大提升你的工作效率. 使用Excel 2007"字段设置"对话框可以控制数据透视表中字段的各种格式.打印.分类汇总和筛选器设置.源名称显示数据源中的字段名称. 自定义名称显示数据透视表中的当前字段名称,如果不存在自定义名称,则显示源名称.要更改"自定义名称",请单击框中的文本并编辑该名称. 布局和打印 "布局"部分 以大纲形式显示项目标签 单击此选项

Win7系统中让垃圾邮件不再进入Outlook中的设置技巧

Outlook 是 Microsoft 的主打邮件传输和协作客户端产品.它是一种集成到Microsoft Office和ExchangeServer中的独立应用程序.Outlook 还提供与Internet Explorer 5.5的交互和集成.电子邮件.日历和联系人管理等功能的完全集成使得 Outlook 成为许多商业用户眼中完美的客户端. Outlook 可帮助您查找和组织信息,以便您可以无缝地使用 Office 应用程序.这有助于您更有效地交流和共享信息.下面小编给大家介绍Win7系统 (

vi 中常用命令技巧

vi中的命令技巧 file copy in the diffrent file: "a9yy      @don't forget the ",a is variable name,9 is copy lines ,yy is copy    "ap        @don't forget the ",a is variable name,p is paste   yy         @copy the current line  p          @pas