Phpquery let PHP handle HTML code as easily as jquery _php instances

Source: Internet
Author: User
Introduction

How to easily parse HTML code in PHP is an estimate of the problems that every phper will encounter. With Phpquery, PHP can be used to handle HTML code as easily as jquery.

Project Address: https://code.google.com/p/phpquery/

GitHub Address: Https://github.com/TobiaszCudnik/phpquery

DEMO

Download Library files: https://code.google.com/p/phpquery/downloads/list

I'm under the OneFile version: Phpquery-0.9.5.386-onefile.zip

Official demo:https://code.google.com/p/phpquery/source/browse/branches/dev/demo.php

It is then referenced in the project.

HTML file test.html:

Copy the Code code as follows:



Spiderman City Drive








spiderman-city Raid





PHP processing :

Copy the Code code as follows:
<?php
Include (' phpquery-onefile.php ');

$filePath = ' test.html ';
$fileContent = file_get_contents ($filePath);
$doc = phpquery::newdocumenthtml ($fileContent);
Phpquery::selectdocument ($doc);
$data = Array (
' Name ' = = Array (),
' href ' = = Array (),
' img ' = = Array ()
);
foreach (PQ (' a ') as $t) {
$href = $t-getattribute (' href ');
$data [' href '] [] = $href;
}
foreach (PQ (' img ') as $img) {
$data [' img '] [] = $domain. $img-getattribute (' src ');
}
foreach (PQ ('. Gamename ') as $name) {
$data [' name '] [] = $name, nodevalue;
}
Var_dump ($data);
?>

The above code contains the fetch attribute and the innertext content (via NodeValue).

Output:

Copy the Code code as follows:
Array (size=3)
' Name ' + =
Array (size=2)
0 = string ' spiderman City Drive ' (length=20)
1 = String ' spiderman-city Raid ' (length=21)
' href ' =
Array (size=2)
0 = String ' http://www.gahe.com/Spiderman-City-Drive ' (length=40)
1 = String ' Http://www.gahe.com/Spiderman-City-Raid ' (length=39)
' img ' =
Array (size=2)
0 = String ' http://www.gahe.com/thumb/12/Spiderman-City-Drive.jpg ' (length=53)
1 = String ' http://www.gahe.com/thumb/12/Spiderman-City-Raid.jpg ' (length=52)

The powerful is the PQ selector, the syntax is similar to jquery, very convenient.

  • Related Article

    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.