国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > PHP > 正文

phpQuery讓php處理html代碼像jQuery一樣方便

2020-03-22 20:18:20
字體:
來源:轉載
供稿:網友
簡介如何在php中方便地解析html代碼,估計是每個phper都會遇到的問題。用phpQuery就可以讓php處理html代碼像jQuery一樣方便。項目地址:https://code.google.com/p/phpquery/github地址:https://github.com/TobiaszCudnik/phpqueryDEMO下載庫文件: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 id="Thumb-13164-3640"
a href="/Spiderman-City-Drive"
img src="/thumb/12/Spiderman-City-Drive.jpg" alt=""
span id="GameName-13164-3640" Spiderman City Drive /span
span id="GameRating-13164-3640"
span /span
/span
/a
/div
div id="Thumb-13169-5946"
a href="/Spiderman-City-Raid"
img src="/thumb/12/Spiderman-City-Raid.jpg" alt=""
span id="GameName-13169-5946" Spiderman - City Raid /span
span id="GameRating-13169-5946"
span /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內容(通過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,很方便。PHP教程

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 镇巴县| 利川市| 建平县| 五原县| 乐陵市| 天全县| 资溪县| 聊城市| 广水市| 庄浪县| 渭南市| 沅江市| 长白| 肃南| 吴堡县| 峨眉山市| 乐都县| 平谷区| 伊宁县| 仁布县| 荥阳市| 阜康市| 慈利县| 承德市| 建宁县| 大城县| 连山| 平谷区| 沧源| 伊春市| 新乡县| 彭州市| 乡城县| 阜康市| 怀化市| 子长县| 泊头市| 获嘉县| 仁化县| 温泉县| 香港|