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


Posted in PHP onJanuary 06, 2015

简介

如何在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内容(通过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 相关文章推荐
无数据库的详细域名查询程序PHP版(4)
Oct 09 PHP
分享PHP入门的学习方法
Jan 02 PHP
php生成随机密码的三种方法小结
Sep 04 PHP
PHP学习笔记之二 php入门知识
Jan 12 PHP
Discuz批量替换帖子内容的方法(使用SQL更新数据库)
Jun 23 PHP
PH P5.2至5.5、5.6的新增功能详解
Jul 14 PHP
2个Codeigniter文件批量上传控制器写法例子
Jul 25 PHP
PHP新建类问题分析及解决思路
Nov 19 PHP
twig模板常用语句实例小结
Feb 04 PHP
Yii框架操作cookie与session的方法实例详解
Sep 04 PHP
php实现大文件断点续传下载实例代码
Oct 01 PHP
PHP实现腾讯短网址生成api接口实例
Dec 08 PHP
php基于表单密码验证与HTTP验证用法实例
Jan 06 #PHP
php使用fputcsv()函数csv文件读写数据的方法
Jan 06 #PHP
phplot生成图片类用法详解
Jan 06 #PHP
写一段简单的PHP建立文件夹代码
Jan 06 #PHP
php读取flash文件高宽帧数背景颜色的方法
Jan 06 #PHP
php自动获取关键字的方法
Jan 06 #PHP
windows7下php开发环境搭建图文教程
Jan 06 #PHP
You might like
php explode函数实例代码
2012/02/27 PHP
PHP封装分页函数实现文本分页和数字分页
2014/10/23 PHP
PHP数据分析引擎计算余弦相似度算法示例
2017/08/08 PHP
php实现映射操作实例详解
2019/10/02 PHP
JS替换文本域内的回车示例
2014/02/18 Javascript
js,jquery滚动/跳转页面到指定位置的实现思路
2014/06/03 Javascript
JavaScript encodeURI 和encodeURIComponent
2015/12/04 Javascript
解析js如何获取css样式
2016/12/11 Javascript
利用Mongoose让JSON数据直接插入或更新到MongoDB
2017/05/03 Javascript
Vue的实例、生命周期与Vue脚手架(vue-cli)实例详解
2017/12/27 Javascript
Angular4 ElementRef的应用
2018/02/26 Javascript
angularJS的radio实现单项二选一的使用方法
2018/02/28 Javascript
vue-image-crop基于Vue的移动端图片裁剪组件示例
2018/08/28 Javascript
vue elementui el-form rules动态验证的实例代码详解
2019/05/23 Javascript
mapboxgl区划标签避让不遮盖实现的代码详解
2020/07/01 Javascript
[01:02:25]2014 DOTA2华西杯精英邀请赛5 24 NewBee VS VG
2014/05/25 DOTA
[53:50]CHAOS vs Mineski 2019国际邀请赛小组赛 BO2 第一场 8.16
2019/08/18 DOTA
python的描述符(descriptor)、装饰器(property)造成的一个无限递归问题分享
2014/07/09 Python
Python实现 多进程导入CSV数据到 MySQL
2017/02/26 Python
python实现多进程代码示例
2018/10/31 Python
Pytorch中accuracy和loss的计算知识点总结
2019/09/10 Python
Python整数与Numpy数据溢出问题解决
2019/09/11 Python
django 外键创建注意事项说明
2020/05/20 Python
Python DES加密实现原理及实例解析
2020/07/17 Python
浅谈Python描述数据结构之KMP篇
2020/09/06 Python
从一次项目重构说起CSS3自定义变量在项目的使用方法
2021/03/01 HTML / CSS
机电专业体育教师求职信
2013/09/21 职场文书
小学生学习感言
2014/03/10 职场文书
校庆筹备方案
2014/03/30 职场文书
新学期开学演讲稿
2014/05/24 职场文书
小学班级特色活动方案
2014/08/31 职场文书
父亲节活动总结
2015/02/12 职场文书
2015大学生入党个人自传
2015/06/26 职场文书
《乘法分配律》教学反思
2016/02/24 职场文书
Windows环境下实现批量执行Sql文件
2021/10/05 SQL Server
docker compose 部署 golang 的 Athens 私有代理问题
2022/04/28 Servers