# htmlparsermodel **Repository Path**: pulin/htmlparsermodel ## Basic Information - **Project Name**: htmlparsermodel - **Description**: No description available - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2017-07-03 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README HtmlParser =============== [![Total Downloads](https://img.shields.io/badge/downloads-9.4k-green.svg)](https://packagist.org/packages/bupt1987/html-parser) [![Build Status](https://api.travis-ci.org/bupt1987/html-parser.svg)](https://travis-ci.org/bupt1987/html-parser) php html解析工具,类似与PHP Simple HTML DOM Parser。 由于基于php模块dom,所以在解析html时的效率比 PHP Simple HTML DOM Parser 快好几倍。 注意:html代码必须是utf-8编码字符,如果不是请转成utf-8 如果有乱码的问题参考:http://www.fwolf.com/blog/post/314 现在支持composer "require": {"bupt1987/html-parser": "dev-master"} 加载composer require 'vendor/autoload.php'; ================================================================================ ##### *Example* ~~~ test

p1

p2

p3

测试1
'; $html_dom = new \HtmlParser\ParserDom($html); $p_array = $html_dom->find('p.test_class'); $p1 = $html_dom->find('p.test_class1',0); $div = $html_dom->find('div#test1',0); foreach ($p_array as $p){ echo $p->getPlainText() . "\n"; } echo $div->getPlainText() . "\n"; echo $p1->getPlainText() . "\n"; echo $p1->getAttr('class') . "\n"; echo "show html:\n"; echo $div->innerHtml() . "\n"; echo $div->outerHtml() . "\n"; ?> ~~~ 基础用法 ================================================================================ ~~~ // 查找所有a标签 $ret = $html->find('a'); // 查找a标签的第一个元素 $ret = $html->find('a', 0); // 查找a标签的倒数第一个元素 $ret = $html->find('a', -1); // 查找所有含有id属性的div标签 $ret = $html->find('div[id]'); // 查找所有含有id属性为foo的div标签 $ret = $html->find('div[id=foo]'); ~~~ 高级用法 ================================================================================ ~~~ // 查找所有id=foo的元素 $ret = $html->find('#foo'); // 查找所有class=foo的元素 $ret = $html->find('.foo'); // 查找所有拥有 id属性的元素 $ret = $html->find('*[id]'); // 查找所有 anchors 和 images标记 $ret = $html->find('a, img'); // 查找所有有"title"属性的anchors and images $ret = $html->find('a[title], img[title]'); ~~~ 层级选择器 ================================================================================ ~~~ // Find all
  • in