简介

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

DEMO

我下的是onefile版:phpQuery-0.9.5.386-onefile.zip

然后在项目中引用。

html文件test.html:

Spiderman City Drive

Spiderman - City Raid

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

最新文章

  1. DIV+CSS圆角边框
  2. FPGA时序案例分析【Vivado版】
  3. 数据数字mongodb 模糊查询以及$type使用
  4. iis7+php_5.5,IIS7+php5.5+fastcgi
  5. Android Selector的简写
  6. H.264编码profile level控制
  7. php中的echo单引号_PHP的数据类型
  8. 前台关于跨域的警告A cookie associated with a cross-site resource at .........,代理服务器
  9. Leetcode--347. 前k个高频元素
  10. linux中字体的安装以及Terminal字体重叠问题解决
  11. 开源操作系统 FreeDOS 二十五年演进史:因微软抛弃 MS-DOS 而来!
  12. BB-UNet:带有包围框先验的U-Net
  13. java web开发实战经典 李兴华_MLDN李兴华JavaWeb开发实战经典(高级案例篇)全部源码...
  14. Ubuntu安装OpenRasp后台管理
  15. 计算机桌面背景不见,电脑桌面背景不见了
  16. 查看mysql汉字占用的字节数_一个汉字在数据库占几个字节
  17. JAVA的Map集合
  18. #创建虚拟机器人URDF模型
  19. python 拟合对数正态分布
  20. 阿丹的1234投资策略

热门文章

  1. dotNET Core WebAPI 统一处理(返回值、参数验证、异常)
  2. C#8.0的两个有趣的新特性以及gRPC
  3. ASP.NET Core Web 项目文件介绍
  4. 微软开源的Trill是什么?
  5. 漫谈单体架构与微服务架构(上):单体架构
  6. 解决C# 7.2中的结构体性能问题
  7. CentOS 7.4 下 如何部署 AspNetCore 结合 consul
  8. DevOps文档中心的技术实践演进
  9. 辅助Visual Studio 2017部署的DevOps新工具
  10. ASP.NET Core 导入导出Excel xlsx 文件