Home > Backend Development > PHP Tutorial > phpQuery allows php to process html code as conveniently as jQuery, phpqueryjquery_PHP tutorial

phpQuery allows php to process html code as conveniently as jQuery, phpqueryjquery_PHP tutorial

WBOY
Release: 2016-07-13 10:10:14
Original
769 people have browsed it

phpQuery allows php to process html code as conveniently as jQuery, phpqueryjquery

Introduction

How to easily parse html code in php is probably a problem that every phper will encounter. Using phpQuery, you can make php process html code as conveniently 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 downloaded the onefile version: phpQuery-0.9.5.386-onefile.zip

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

Then reference it in the project.

html file test.html:

Copy code The code is as follows:
php processing

:

Copy code The code is as follows:
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 includes fetching attributes and innerText content (fetching through nodeValue).

Output:

Copy code The code is 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 most powerful one is the pq selector. Its syntax is similar to jQuery, which is very convenient.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/938850.htmlTechArticlephpQuery allows php to process html code as conveniently as jQuery. phpqueryjquery introduces how to parse html code conveniently in php, estimated This is a problem that every phper will encounter. Just use phpQuery...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template