Parse the HTML code with phpquery like jquery, Phpqueryjquery
Brief 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:
<Divclass= "thumb"ID= "thumb-13164-3640"style= "position:absolute; left:0px; top:0px;"> <ahref= "/spiderman-city-drive"> <imgsrc= "/thumb/12/spiderman-city-drive.jpg"alt=""> <spanclass= "Gamename"ID= "gamename-13164-3640"style= "Display:none;">Spiderman City Drive
span> <spanclass= "Gamerating"ID= "gamerating-13164-3640"style= "Display:none;"> <spanstyle= "WIDTH:68.14816PX;">
span>
span>
a>
Div><Divclass= "thumb"ID= "thumb-13169-5946"style= "position:absolute; left:190px; top:0px;"> <ahref= "/spiderman-city-raid"> <imgsrc= "/thumb/12/spiderman-city-raid.jpg"alt=""> <spanclass= "Gamename"ID= "gamename-13169-5946"style= "Display:none;">Spiderman-city Raid
span> <span class= "gamerating" ID = "gamerating-13169-5946" style = "Display:none;" > < span style = "WIDTH:67.01152PX;" >
span >
span>
a>
Div >
PHP Processing:
Phpinclude(' 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:
Array (size=3) ' Name ' = = array (size=2 )string ' spiderman City Drive ' (length=20) string ' spiderman-city Raid ' (length=21) ' href ' = = array (size=2 ) string ' http://www.gahe.com/Spiderman-City-Drive ' (length=40) String ' Http://www.gahe.com/Spiderman-City-Raid ' (length=39) ' img ' = = array (size=2) string ' http://www.gahe.com/thumb/12/Spiderman-City-Drive.jpg ' (length =53) 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.
http://www.bkjia.com/PHPjc/938095.html www.bkjia.com true http://www.bkjia.com/PHPjc/938095.html techarticle with phpquery like jquery parsing html code, phpqueryjquery about how to easily parse HTML code in PHP, it is estimated that every phper will encounter problems. With Phpquery can let PHP ...