weijihao / php-htmldom
这是一个HTML DOM插件
0.1.3
2018-07-26 10:41 UTC
This package is auto-updated.
Last update: 2024-09-13 23:44:43 UTC
README
PHP编写的HTMLDOM
##安装步骤##
1、确保已经安装了Composer或者下载了composer.phar
composer require weijihao/php-htmldom
或者
在composer.json文件添加如下代码:
"require": { "weijihao/php-htmldom": "^0.1.0" }
##调用插件##
1、在CakePHP中调用
use Weijihao\HtmlDom\HtmlDom; ... $htmlDom = new Weijihao\HtmlDom\HtmlDom(); $html = $htmlDom->str_get_html( $str ); //or $html = $htmlDom->file_get_html( $file_name ); $elems = $html->find($elem_name); ...
2、在根目录创建调用实例文件index.php,代码如下:
require_once "vendor/autoload.php"; $htmlDom = new Weijihao\HtmlDom\HtmlDom(); $html = $htmlDom->str_get_html('<div id="hello">Hello</div><div id="world">World</div>'); $html->find('div', 1)->class = 'bar'; echo $html->find('div[id=hello]', 0)->innertext; echo "\n <br/>"; $html->find('div[id=hello]', 0)->innertext = 'foo'; echo $html; // Output: <div id="hello">foo</div><div id="world" class="bar">World</div> $htmlDom->dump_html_tree($html); // $htmlDom = new Weijihao\HtmlDom\HtmlDom(); $htmDom = $htmlDom->file_get_html("http://www.baidu.com"); foreach ($htmDom->find('a') as $element) { echo $element->href . $element->innertext . '<br>'; }