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 相关文章推荐
域名查询代码公布
Oct 09 PHP
使用sockets:从新闻组中获取文章(一)
Oct 09 PHP
PHP经典的给图片加水印程序
Dec 06 PHP
防止用户利用PHP代码DOS造成用光网络带宽
Mar 01 PHP
PHP输出XML到页面的3种方法详解
Jun 06 PHP
php pki加密技术(openssl)详解
Jul 01 PHP
详解php设置session(过期、失效、有效期)
Nov 12 PHP
Yii实现显示静态页的方法
Apr 25 PHP
php while循环控制的简单实例
May 30 PHP
PHP处理CSV表格文件的常用操作方法总结
Jul 01 PHP
PHP登录(ajax提交数据和后台校验)实例分享
Dec 29 PHP
php实现的rc4加密解密类定义与用法示例
Aug 16 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实现用session来实现记录用户登陆信息
2018/10/15 PHP
Laravel 微信小程序后端实现用户登录的示例代码
2019/11/26 PHP
用脚本调用样式的几种方法
2006/12/09 Javascript
js获取提交的字符串的字节数
2009/02/09 Javascript
jquery对单选框,多选框,文本框等常见操作小结
2014/01/08 Javascript
Jquery中ajax方法data参数的用法小结
2014/02/12 Javascript
webpack打包后直接访问页面图片路径错误的解决方法
2017/06/17 Javascript
微信小程序自定义导航教程(兼容各种手机)
2018/12/12 Javascript
JS温故而知新之变量提升和时间死区
2019/01/27 Javascript
layui树形菜单动态遍历的例子
2019/09/23 Javascript
ElementUI之Message功能拓展详解
2019/10/18 Javascript
微信小程序转化为uni-app项目的方法示例
2020/05/22 Javascript
vue使用svg文件补充-svg放大缩小操作(使用d3.js)
2020/09/22 Javascript
Python中字符串对齐方法介绍
2015/05/21 Python
python实现字典(dict)和字符串(string)的相互转换方法
2017/03/01 Python
基于Linux系统中python matplotlib画图的中文显示问题的解决方法
2017/06/15 Python
Python字典操作详细介绍及字典内建方法分享
2018/01/04 Python
python 读写excel文件操作示例【附源码下载】
2019/06/19 Python
用Python实现BP神经网络(附代码)
2019/07/10 Python
Html5实现单张、多张图片上传功能
2019/04/28 HTML / CSS
香港化妆品经销商:我的公主
2016/08/05 全球购物
Expedia意大利旅游网站:酒店、机票和租车预订
2017/10/30 全球购物
工作中的自我评价如何写好
2013/10/28 职场文书
师范生实习自我鉴定
2013/11/01 职场文书
两则小学生的自我评价分享
2013/11/14 职场文书
募捐倡议书
2014/04/14 职场文书
优秀党支部书记事迹材料
2014/05/29 职场文书
会计人员演讲稿
2014/09/11 职场文书
2014年党的群众路线教育实践活动整改措施(个人版)
2014/09/25 职场文书
大学生自我评价范文
2015/03/03 职场文书
大学生安全教育主题班会
2015/08/12 职场文书
2016年党校科级干部培训班学习心得体会
2016/01/06 职场文书
解决Pytorch半精度浮点型网络训练的问题
2021/05/24 Python
带你学习MySQL执行计划
2021/05/31 MySQL
Python中Schedule模块使用详解 周期任务神器
2022/04/19 Python
SQL Server中的逻辑函数介绍
2022/05/25 SQL Server