phpQuery讓php處理html代碼像jQuery一樣方便
簡(jiǎn)介
如何在php中方便地解析html代碼,估計(jì)是每個(gè)phper都會(huì)遇到的問(wèn)題。用phpQuery就可以讓php處理html代碼像jQuery一樣方便。
項(xiàng)目地址:https://code.google.com/p/phpquery/
github地址:https://github.com/TobiaszCudnik/phpquery
DEMO
下載庫(kù)文件: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
然后在項(xiàng)目中引用。
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)容(通過(guò)nodeValue?。?/p>
輸出:
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)
強(qiáng)大的是pq選擇器,語(yǔ)法類似jQuery,很方便。
- jQuery如何將選中的對(duì)象轉(zhuǎn)化為原始的DOM對(duì)象
- jQuery獲取單擊節(jié)點(diǎn)對(duì)象的方法
- jquery遍歷json對(duì)象集合詳解
- jQuery對(duì)象的鏈?zhǔn)讲僮饔梅ǚ治?/a>
- 解析Jquery中如何把一段html代碼動(dòng)態(tài)寫入到DIV中(實(shí)例說(shuō)明)
- jQuery實(shí)現(xiàn)鼠標(biāo)跟隨提示層效果代碼(可顯示文本,Div,Table,Html等)
- jquery 獲取 outerHtml 包含當(dāng)前節(jié)點(diǎn)本身的代碼
- JQuery獲取與設(shè)置HTML元素的內(nèi)容或文本的實(shí)現(xiàn)代碼
- jquery模擬LCD 時(shí)鐘的html文件源代碼
- Jquery在指定DIV加載HTML示例代碼
- jquery隨意添加移除html的實(shí)現(xiàn)代碼
- jQuery查看選中對(duì)象HTML代碼的方法
相關(guān)文章
PHP將英文數(shù)字轉(zhuǎn)換為阿拉伯?dāng)?shù)字實(shí)例講解
在本篇文章里小編給大家分享了關(guān)于PHP將英文數(shù)字轉(zhuǎn)換為阿拉伯?dāng)?shù)字實(shí)例內(nèi)容,有興趣的朋友們可以參考學(xué)習(xí)下。2019-01-01PHP 實(shí)現(xiàn)人民幣小寫轉(zhuǎn)換成大寫的方法及大小寫轉(zhuǎn)換函數(shù)
這篇文章主要介紹了PHP 實(shí)現(xiàn)人民幣小寫轉(zhuǎn)換成大寫的方法,大小寫轉(zhuǎn)換函數(shù)的使用方法介紹,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-11-11tp5.0框架隱藏index.php入口文件及模塊和控制器的方法分析
這篇文章主要介紹了tp5.0框架隱藏index.php入口文件及模塊和控制器的方法,結(jié)合實(shí)例形式分析thinkPHP5.0隱藏index.php入口文件及模塊和控制器的具體配置方法、操作技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2020-02-02PHP FTP操作類代碼( 上傳、拷貝、移動(dòng)、刪除文件/創(chuàng)建目錄)
這篇文章主要介紹了php中實(shí)現(xiàn)FTP功能,包括上傳,拷貝,移動(dòng),刪除文件與創(chuàng)建目錄功能,需要的朋友可以參考下2014-05-05SQL+HTML+PHP?一個(gè)簡(jiǎn)單論壇網(wǎng)站的綜合開(kāi)發(fā)案例(注冊(cè)、登錄、注銷、修改信息、留言等)
這篇文章主要介紹了SQL+HTML+PHP?一個(gè)簡(jiǎn)單論壇網(wǎng)站的綜合開(kāi)發(fā)案例(注冊(cè)、登錄、注銷、修改信息、留言等),需要的朋友可以參考下2022-12-12laravel實(shí)現(xiàn)簡(jiǎn)單用戶權(quán)限的示例代碼
這篇文章主要介紹了laravel實(shí)現(xiàn)簡(jiǎn)單用戶權(quán)限的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05ThinkPHP中pathinfo的訪問(wèn)模式、路徑訪問(wèn)模式及URL重寫總結(jié)
這篇文章主要介紹了ThinkPHP中pathinfo的訪問(wèn)模式、路徑訪問(wèn)模式及URL重寫總結(jié),是ThinkPHP路由訪問(wèn)的基礎(chǔ)知識(shí),在ThinkPHP開(kāi)發(fā)中非常重要,需要的朋友可以參考下2014-08-08基于PHP的簡(jiǎn)單采集數(shù)據(jù)入庫(kù)程序
前幾天有一朋友要我?guī)妥鲆粋€(gè)采集新聞信息的程序,抽了點(diǎn)時(shí)間寫了個(gè)PHP版本的,隨筆記錄下。2014-07-07