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.
http://www.bkjia.com/PHPjc/938850.htmlwww.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...