如何在php中方便的解析html代码,估计是每个PHP都会遇到的问题。有了phpQuery,php可以像jQuery一样方便地处理html代码。
项目地址:https://code.google.com/p/phpquery/.
Github地址:https://github.com/TobiaszCudnik/phpquery
演示
下载库文件:https://code.google.com/p/phpquery/downloads/list
我下载了onefile版本:phpquery-0.9.5.386-onefile.zip。
官方演示:https://code . Google . com/p/PHP query/source/browse/branches/dev/demo . PHP。
然后在项目中引用它。
html文件test.html:
代码如下:
Spiderman City Drive
Spiderman - City Raid
蜘蛛侠-城市突袭
代码如下:
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);
?>
array(),
'href' => array(),
' img ' = > array()
);
foreach(pq(' a ')as $ t){
$ href = $ t--> get attribute(' href ');
$ data[' href '][]= $ href;
}
foreach(pq(' img ')as $ img){
$ data[' img '][]= $ domain。$ img-> get attribute(' src ');
}
foreach(pq('。GameName ')as $ name){
$ data[' name '][]= $ name-> nodeValue;
}
var _ dump($ data);
& # 63;>
输出:
代码如下:
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.comhttps://www.php.cn/thumb/12/Spiderman-City-Drive.jpg' (length=53)
1 => string 'http://www.gahe.comhttps://www.php.cn/thumb/12/Spiderman-City-Raid.jpg' (length=52)
array(size = 3)
' name ' = >
array(size = 2)
0 = > string ' Spiderman City Drive '(length = 20)
1 = > string ' Spiderman-City Raid '(length = 21)
' href ' = >
array(size = 1