phpQuery讓php處理html代碼像jQuery一樣方便
簡介
如何在php中方便地解析html代碼,估計是每個phper都會遇到的問題。用phpQuery就可以讓php處理html代碼像jQuery一樣方便。
項目地址:https://code.google.com/p/phpquery/
github地址:https://github.com/TobiaszCudnik/phpquery
DEMO
下載庫文件:https://code.google.com/p/phpquery/downloads/list
我下的是onefile版:phpQuery-0.9.5.386-onefile.zip
官方demo:https://code.google.com/p/phpquery/source/browse/branches/dev/demo.php
然后在項目中引用。
html文件test.html:
<div class="thumb" id="Thumb-13164-3640" style="position: absolute; left: 0px; top: 0px;">
<a href="/Spiderman-City-Drive">
<img src="/thumb/12/Spiderman-City-Drive.jpg" alt="">
<span class="GameName" id="GameName-13164-3640" style="display: none;">Spiderman City Drive</span>
<span class="GameRating" id="GameRating-13164-3640" style="display: none;">
<span style="width: 68.14816px;"></span>
</span>
</a>
</div>
<div class="thumb" id="Thumb-13169-5946" style="position: absolute; left: 190px; top: 0px;">
<a href="/Spiderman-City-Raid">
<img src="/thumb/12/Spiderman-City-Raid.jpg" alt="">
<span class="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處理:
<?php
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);
?>
上面的代碼中包含了取屬性和innerText內(nèi)容(通過nodeValue?。?。
輸出:
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)
強大的是pq選擇器,語法類似jQuery,很方便。
- jQuery如何將選中的對象轉(zhuǎn)化為原始的DOM對象
- jQuery獲取單擊節(jié)點對象的方法
- jquery遍歷json對象集合詳解
- jQuery對象的鏈式操作用法分析
- 解析Jquery中如何把一段html代碼動態(tài)寫入到DIV中(實例說明)
- jQuery實現(xiàn)鼠標跟隨提示層效果代碼(可顯示文本,Div,Table,Html等)
- jquery 獲取 outerHtml 包含當前節(jié)點本身的代碼
- JQuery獲取與設置HTML元素的內(nèi)容或文本的實現(xiàn)代碼
- jquery模擬LCD 時鐘的html文件源代碼
- Jquery在指定DIV加載HTML示例代碼
- jquery隨意添加移除html的實現(xiàn)代碼
- jQuery查看選中對象HTML代碼的方法
相關(guān)文章
Laravel中任務調(diào)度console使用方法小結(jié)
這篇文章主要給大家簡單介紹了Laravel中任務調(diào)度console使用方法,并附上一個簡單的示例,希望對大家學習使用console能夠有所幫助2017-05-05
Yii框架ACF(accessController)簡單權(quán)限控制操作示例
這篇文章主要介紹了Yii框架ACF(accessController)簡單權(quán)限控制操作,結(jié)合實例形式分析了Yii框架簡單權(quán)限控制操作參數(shù)設置與使用技巧,需要的朋友可以參考下2019-04-04
從零開始學YII2框架(五)快速生成代碼工具 Gii 的使用
用過Yii1框架的Coder都知道,Gii可以為你快速生成代碼,也就是說搭建一個可以增刪改查的WebApp可能一行代碼都不用寫。上次介紹了如何安裝Yii框架,本次介紹一下如何使用gii工具快速實現(xiàn)CRUD功能。2014-08-08

