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 相关文章推荐
用cookies来跟踪识别用户
Oct 09 PHP
让你的PHP同时支持GIF、png、JPEG
Oct 09 PHP
PHP下通过系统信号量加锁方式获取递增序列ID
Sep 25 PHP
延长phpmyadmin登录时间的方法
Feb 06 PHP
php中计算中文字符串长度、截取中文字符串的函数代码
Aug 09 PHP
基于Zookeeper的使用详解
May 02 PHP
php常用Output和ptions/Info函数集介绍
Jun 19 PHP
解析coreseek for sphinx的使用
Jun 21 PHP
php 一维数组的循环遍历实现代码
Apr 10 PHP
laravel 5.4中实现无限级分类的方法示例
Jul 27 PHP
购物车实现的几种方式优缺点对比
May 02 PHP
PHP 进程池与轮询调度算法实现多任务的示例代码
Nov 26 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
《心理测量者3》剧场版动画预告
2020/03/02 日漫
关于PHP5 Session生命周期介绍
2010/03/02 PHP
php中实现记住密码自动登录的代码
2011/03/02 PHP
php获取当前页面完整URL地址
2015/12/30 PHP
PHP 实现人民币小写转换成大写的方法及大小写转换函数
2017/11/17 PHP
phpstorm最新激活码分享亲测phpstorm2020.2.3版可用
2020/11/22 PHP
MooBox 基于Mootools的对话框插件
2012/01/20 Javascript
javascript loadScript异步加载脚本示例讲解
2013/11/14 Javascript
JS对象转换为Jquery对象示例
2014/01/26 Javascript
使用jQuery时Form表单元素ID和name命名大忌
2014/03/06 Javascript
Jquery通过JSON字符串创建JSON对象
2014/08/24 Javascript
JavaScript判断是否为数字的4种方法及效率比较
2015/04/01 Javascript
jQuery+php实时获取及响应文本框输入内容的方法
2016/05/24 Javascript
利用Console来Debug的10个高级技巧汇总
2018/03/26 Javascript
使用typescript开发angular模块并发布npm包
2018/04/19 Javascript
详解小程序rich-text对富文本支持方案
2018/11/28 Javascript
Vue路由之JWT身份认证的实现方法
2019/08/26 Javascript
Layui数据表格判断编辑输入的值,是否为我需要的类型详解
2019/10/26 Javascript
JS实现简易留言板特效
2019/12/23 Javascript
Handtrack.js库实现实时监测手部运动(推荐)
2021/02/08 Javascript
python爬虫中get和post方法介绍以及cookie作用
2018/02/08 Python
Python3.4实现远程控制电脑开关机
2018/02/22 Python
python实现微信自动回复功能
2018/04/11 Python
pandas每次多Sheet写入文件的方法
2018/12/10 Python
ZABBIX3.2使用python脚本实现监控报表的方法
2019/07/02 Python
Python函数的默认参数设计示例详解
2019/12/01 Python
意大利奢侈品牌在线精品店:Jole.it
2020/11/23 全球购物
C#实现对任一张表的数据进行增,删,改,查要求,运用Webservice,体现出三层架构
2014/07/11 面试题
Unix控制后台进程都有哪些进程
2016/09/22 面试题
中专生毕业自我鉴定
2013/11/01 职场文书
2014年学前班工作总结
2014/12/08 职场文书
新员工入职欢迎词
2015/01/23 职场文书
2015年维修工作总结
2015/04/25 职场文书
朋友离别感言
2015/08/04 职场文书
小学美术教学反思
2016/02/17 职场文书
入党转正申请书范文
2019/05/20 职场文书