官方主页 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:
<p class="sycode"> $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 " ;} </p>
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 一样来连缀方法)
<p class="sycode"> <pre class="sycode" name="code"> <p class="sycode"> qp(QueryPath :: HTML_STUB) -> find( ' body ' ) -> text( ' Hello World ' ) -> writeHTML(); </p>
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 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:
<p class="sycode"> <! 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 > </p>
Most of that HTML comes from the QueryPath::HTML_STUB. All we did was add the Hello World text inside of the
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 ExampleThis 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.
<p class="sycode"> <? 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 id="This-is-a-test-page">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(); ?> </p>
The code above produces the following HTML:
代码
<p class="sycode"> <! 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 > </p>
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 它吧~~!

Calculating the total number of elements in a PHP multidimensional array can be done using recursive or iterative methods. 1. The recursive method counts by traversing the array and recursively processing nested arrays. 2. The iterative method uses the stack to simulate recursion to avoid depth problems. 3. The array_walk_recursive function can also be implemented, but it requires manual counting.

In PHP, the characteristic of a do-while loop is to ensure that the loop body is executed at least once, and then decide whether to continue the loop based on the conditions. 1) It executes the loop body before conditional checking, suitable for scenarios where operations need to be performed at least once, such as user input verification and menu systems. 2) However, the syntax of the do-while loop can cause confusion among newbies and may add unnecessary performance overhead.

Efficient hashing strings in PHP can use the following methods: 1. Use the md5 function for fast hashing, but is not suitable for password storage. 2. Use the sha256 function to improve security. 3. Use the password_hash function to process passwords to provide the highest security and convenience.

Implementing an array sliding window in PHP can be done by functions slideWindow and slideWindowAverage. 1. Use the slideWindow function to split an array into a fixed-size subarray. 2. Use the slideWindowAverage function to calculate the average value in each window. 3. For real-time data streams, asynchronous processing and outlier detection can be used using ReactPHP.

The __clone method in PHP is used to perform custom operations when object cloning. When cloning an object using the clone keyword, if the object has a __clone method, the method will be automatically called, allowing customized processing during the cloning process, such as resetting the reference type attribute to ensure the independence of the cloned object.

In PHP, goto statements are used to unconditionally jump to specific tags in the program. 1) It can simplify the processing of complex nested loops or conditional statements, but 2) Using goto may make the code difficult to understand and maintain, and 3) It is recommended to give priority to the use of structured control statements. Overall, goto should be used with caution and best practices are followed to ensure the readability and maintainability of the code.

In PHP, data statistics can be achieved by using built-in functions, custom functions, and third-party libraries. 1) Use built-in functions such as array_sum() and count() to perform basic statistics. 2) Write custom functions to calculate complex statistics such as medians. 3) Use the PHP-ML library to perform advanced statistical analysis. Through these methods, data statistics can be performed efficiently.

Yes, anonymous functions in PHP refer to functions without names. They can be passed as parameters to other functions and as return values of functions, making the code more flexible and efficient. When using anonymous functions, you need to pay attention to scope and performance issues.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
