日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > php >内容正文

php

$query php,phpQuery让php处理html代码像jQuery一样方便

發(fā)布時(shí)間:2023/12/4 php 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 $query php,phpQuery让php处理html代码像jQuery一样方便 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

簡(jiǎn)介

如何在php中方便地解析html代碼,估計(jì)是每個(gè)phper都會(huì)遇到的問題。用phpQuery就可以讓php處理html代碼像jQuery一樣方便。

DEMO

我下的是onefile版:phpQuery-0.9.5.386-onefile.zip

然后在項(xiàng)目中引用。

html文件test.html:

???????

Spiderman City Drive

???????

Spiderman - City Raid

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)

強(qiáng)大的是pq選擇器,語(yǔ)法類似jQuery,很方便。

總結(jié)

以上是生活随笔為你收集整理的$query php,phpQuery让php处理html代码像jQuery一样方便的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。