Parse html code using phpQuery like jquery

Source: Internet
Author: User
: This article describes how to use phpQuery to parse html code like jquery. For more information about PHP tutorials, see. Introduction

How to easily parse html code in php is probably a problem for every phper. PhpQuery makes it as convenient for php to process html code as jQuery.

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

Github address: https://github.com/TobiaszCudnik/phpquery

DEMO

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

I'm using onefile: phpQuery-0.9.5.386-onefile.zip

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

And then reference it in the project.

HTML file test.html:

Spiderman City Drive

Spiderman - City Raid

Php processing:

  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 code above contains the Get attribute and innerText content (obtained using nodeValue ).

Output:

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)

Powerful is the pq selector. The syntax is similar to jQuery, which is very convenient.

The above describes how to use phpQuery to parse html code like jquery, including some content. I hope my friends who are interested in PHP tutorials will be helpful.

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.