加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 02:19:41 所属栏目:PHP教程 来源:网络整理
导读:《PHP实战:phpQuery让php处理html代码像jQuery一样方便》要点: 本文介绍了PHP实战:phpQuery让php处理html代码像jQuery一样方便,希望对您有用。如果有疑问,可以联系我们。 简介 PHP实例 如安在php中方便地解析html代码,估计是每个phper都会遇到的问题.用

《PHP实战:phpQuery让php处理html代码像jQuery一样方便》要点:
本文介绍了PHP实战:phpQuery让php处理html代码像jQuery一样方便,希望对您有用。如果有疑问,可以联系我们。

简介PHP实例

如安在php中方便地解析html代码,估计是每个phper都会遇到的问题.用phpQuery就可以让php处理html代码像jQuery一样方便.PHP实例

项目地址:https://code.google.com/p/phpquery/PHP实例

github地址:https://github.com/TobiaszCudnik/phpqueryPHP实例

DEMOPHP实例

下载库文件:https://code.google.com/p/phpquery/downloads/listPHP实例

我下的是onefile版:phpQuery-0.9.5.386-onefile.zipPHP实例

官方demo:https://code.google.com/p/phpquery/source/browse/branches/dev/demo.phpPHP实例

然后在项目中引用.PHP实例

html文件test.htmlPHP实例

代码如下:

<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实例

代码如下:

<?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取).PHP实例

输出:PHP实例

代码如下:

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实例

编程之家培训学院每天发布《PHP实战:phpQuery让php处理html代码像jQuery一样方便》等实战技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培养人才。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读