用phpQuery像jquery一样解析html代码,phpqueryjquery_PHP教程
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了用phpQuery像jquery一样解析html代码,phpqueryjquery_PHP教程,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2646字,纯文字阅读大概需要4分钟。
内容图文
用phpQuery像jquery一样解析html代码,phpqueryjquery
简介
如何在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,很方便。
http://www.bkjia.com/PHPjc/938095.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/938095.htmlTechArticle用phpQuery像jquery一样解析html代码,phpqueryjquery 简介 如何在php中方便地解析html代码,估计是每个phper都会遇到的问题。用phpQuery就可以让php处...
内容总结
以上是互联网集市为您收集整理的用phpQuery像jquery一样解析html代码,phpqueryjquery_PHP教程全部内容,希望文章能够帮你解决用phpQuery像jquery一样解析html代码,phpqueryjquery_PHP教程所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。