phpquery-implementation of PHP based on jquery

Source: Internet
Author: User
Tags xpath

Reproduced in: http://www.cnblogs.com/in-loading/archive/2012/04/11/2442697.html

The power of the selector of query is obvious to all, Phpquery let PHP also have this ability, it is equivalent to the service side of jquery.

Let's take a look at the official profile:

Phpquery is a server-side, chainable, CSS3 selector driven Document Object Model (DOM) API based on JQuery JavaScript LIBR ary.

The Library is written in PHP5 and provides additional Command line Interface (CLI).

The meaning of existence

We sometimes need to crawl the content of a Web page, but only the specific part of the information, usually with the regular to solve, of course, there is no problem. Regular is a general-purpose solution, but in certain situations, there are often simpler and quicker methods. For example, you want to query a programming problem, of course you can use Google, but StackOverflow as a professional programming community, will provide you with more, more reliable answers.

For HTML pages, there are 3 main reasons why you should not use regular

1, writing conditional expressions more trouble
Especially for beginners, see a bunch of "unintelligible" word Fu Yi together, a kind of head to explode the feeling. If the object to be detached is not too obvious, it is more cumbersome to write.

2, the efficiency is not high
For PHP, the regular should be no way, can be solved by the string function, do not bother the regular. Using the regular to deal with a file of more than 30 K, efficiency is not guaranteed.

3, there are phpquery
If you've ever used jquery, it should be easy to get a particular element, Phpquery makes it possible.

Brief analysis of Phpquery

Phpquery is a newly added domdocument based on PHP5. The DOMDocument is specifically designed to handle html/xml. It provides a powerful XPath selector and many other html/xml operation functions, making it easy to handle html/xml. Then why not use it directly? This, to see the official website of the list of functions to know, if you have confidence in their own memory, may wish to try.

A few simple examples

Get the hottest jobs in blue ideal

    1. <?
    2. Include ' phpquery.php ';
    3. Phpquery::newdocumentfile (' http://job.blueidea.com ');
    4. $companies = PQ (' #hotcoms. COMs ')->find (' div ');
    5. foreach ($companies as $company)
    6. {
    7. Echo PQ ($company)->find (' H3 a ')->text ()."  <br> ";
    8. }

Summary
    • PQ () is like a $ () in jquery
    • Basically the jquery selector can be used on phpquery, just put '. ' Become '--'
    • Phpquery provides several ways to load files, some use strings, some use files (including URLs), and choose to pay attention to
    • Basically, this page is going to explain the problem.
Other parsers

Simplehtmldom is also a good HTML parser, it is very convenient to use, is based on the regular, so no phpquery so strong, if not too high requirements, but also basic enough.

YQL is a yahoo out of a SQL-like language, to obtain the corresponding data, is also very powerful, without any class library, can be called directly, support XPath, if the SQL statement is familiar with, you can consider yql.

>>querypath, jquery on PHP

-----------------------------------------------------

Regular expressions are usually used when Web pages are collected. But sometimes for the students who are not very good, such as me, it is a cup. Now Google's project has a phpquery, as the name suggests query, completely similar to the jquery syntax, but this is the server side, in general, it is possible to use PHP directly to collect the corresponding page content, it is really too convenient, it makes everything possible ...

Phpquery is a server-side, chainable, CSS3 selector driven Document Object Model (DOM) API based on JQuery JAVASC Ript Library.

The Library is written in PHP5 and provides additional Command line Interface (CLI).

Project: http://code.google.com/p/phpquery/

If you've ever used jquery, you'll see that it's all like this.

How to get the title of the webpage quickly and conveniently?

Phpquery-a jquery-based PHP implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.