phpQuery让php处理html代码像jQuery一样方便
简介
如何在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,很方便。
- jQuery如何将选中的对象转化为原始的DOM对象
- jQuery获取单击节点对象的方法
- jquery遍历json对象集合详解
- jQuery对象的链式操作用法分析
- 解析Jquery中如何把一段html代码动态写入到DIV中(实例说明)
- jQuery实现鼠标跟随提示层效果代码(可显示文本,Div,Table,Html等)
- jquery 获取 outerHtml 包含当前节点本身的代码
- JQuery获取与设置HTML元素的内容或文本的实现代码
- jquery模拟LCD 时钟的html文件源代码
- Jquery在指定DIV加载HTML示例代码
- jquery随意添加移除html的实现代码
- jQuery查看选中对象HTML代码的方法
相关文章
Laravel框架基于中间件实现禁止未登录用户访问页面功能示例
这篇文章主要介绍了Laravel框架基于中间件实现禁止未登录用户访问页面功能,结合实例形式分析了Laravel框架中间件生成、注册及使用相关操作技巧,需要的朋友可以参考下2019-01-01三个思路解决laravel上传文件报错:413 Request Entity Too Large问题
上传图片的时候,是用laravel自带的上传图片的方法,一下气上传了20张,结果就无情报错:413 Request Entity Too Large,后面查一下,这个报错信息是nginx报的错误,不是php报的错误。也就是说在上传图片的时候被nginx拦截了2017-11-11yii2 commands模式以及配置crontab定时任务的方法
下面小编就为大家带来一篇yii2 commands模式以及配置crontab定时任务的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-08-08
最新评论