简介

如何在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,很方便。

用phpQuery像jquery一样解析html代码的更多相关文章

  1. jquery eval解析JSON中的注意点介绍

    在JS中将JSON的字符串解析成JSON数据格式,一般有两种方式:使用eval()函数.使用Function对象来进行返回解析,下面有个示例,感兴趣的朋友可以参考下   在JS中将JSON的字符串解析 ...

  2. jQuery ajax解析xml文件demo

    解析xml文件,然后将城市列表还原到下拉列表框中:当选择下拉列表框时,在对应的文本框中显示该城市信息. 前端代码: <!doctype html> <html> <hea ...

  3. jquery json解析详解

    我们先以解析上例中的comments对象的JSON数据为例,然后再小结jQuery中解析JSON数据的方法. JSON数据如下,是一个嵌套JSON: 1 {"comments":[ ...

  4. 可控制导航下拉方向的jQuery下拉菜单代码

    效果:http://hovertree.com/texiao/nav/1/ 代码如下: <!DOCTYPE html> <html> <head> <meta ...

  5. jquery树形菜单完整代码

    本实例实现了树形的动态菜单,兼容IE8,火狐,Chrome等浏览器.使用了jQuery的toggle() 方法.效果和代码如下: <!DOCTYPE html PUBLIC "-//W ...

  6. 一步步教你为网站开发Android客户端---HttpWatch抓包,HttpClient模拟POST请求,Jsoup解析HTML代码,动态更新ListView

    本文面向Android初级开发者,有一定的Java和Android知识即可. 文章覆盖知识点:HttpWatch抓包,HttpClient模拟POST请求,Jsoup解析HTML代码,动态更新List ...

  7. 分析和解析PHP代码的7大工具

    PHP已成为时下最热门的编程语言之一,然而却有许多PHP程序员苦恼找不到合适的工具来帮助自己分析和解析PHP代码.今天小编就为大家介绍几个非常不错的工具,来帮助程序员们提高自己的工作效率,一起来看看吧 ...

  8. 10 个实用的 jQuery 表单操作代码片段

    jQuery 绝对是一个伟大的开源JavaScript类库,是帮助我们快速和高效开发前端应用的利器.可能大家在日常的开发过程中常常会处理表单相关的 JavaScript,在今天这篇代码片段分享文章中, ...

  9. jQuery select的操作代码

    jQuery對select的操作的实际应用代码. //改變時的事件  复制代码代码如下: $("#testSelect").change(function(){ //事件發生  j ...

随机推荐

  1. (转载)Javascript 进阶 作用域 作用域链

    载请标明出处:http://blog.csdn.net/lmj623565791/article/details/25076713 一直觉得Js很强大,由于长期不写js代码,最近刚好温故温故. 1.J ...

  2. Redis Admin UI

    https://github.com/ServiceStackApps/RedisAdminUI 最近的v4版本不能用,需要下载v3版本,下载地址 https://github.com/Service ...

  3. tomcat的webapp下的root文件夹的作用是什么

    1.基本一样..只是表示不同的tomcat的http路径而已. root目录默认放的是tomcat自己的一个项目,如:http://localhost:8080/默认访问root项目 对于webapp ...

  4. 解决NSAttributedString与UILabel高度自适应计算问题

    两个类扩展方法: /** *  修改富文本的颜色 * *  @param str   要改变的string *  @param color 设置颜色 *  @param range 设置颜色的文字范围 ...

  5. Asp与Asp.Net的区别

    今天在网上看到一位朋友问asp与asp.net的区别.编辑本人也是从asp转型到.net来的,几年了,几乎都忘记了asp的存在,也说不出它们之间的区别,因为感觉两者是根本就没有联系,非要说有联系,那就 ...

  6. 微软SQLHelper.cs类

    using System; using System.Data; using System.Xml; using System.Data.SqlClient; using System.Collect ...

  7. 《chkconfig命令》-linux命令五分钟系列之四

    本原创文章属于<Linux大棚>博客. 博客地址为http://roclinux.cn. 文章作者为roc 希望您能通过捐款的方式支持Linux大棚博客的运行和发展.请见“关于捐款” == ...

  8. ICE学习第一步-----配置ICE环境变量

    安装 ICE: 1.下载ICE: http://www.zeroc.com/download.html 下载说明:ICE支持语言(C++, Java, C#, Visual Basic,Python, ...

  9. php函数——『解析 xml数据』

    <?php //该文件是 //$raw_post_data = file_get_contents('php://input'); //file_put_contents('a.txt', $r ...

  10. Android各种访问权限Permission详解

    原文:http://jingyan.baidu.com/article/afd8f4de4688af34e386e976.html 在Android的设计中,资源的访问或者网络连接,要得到这些服务都需 ...