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