: 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.