我正在尝试从本地HTML文件中提取内容。我使用HTML解析方法,但什么都没有。
PHP代码如下:
$doc = new \DOMDocument();
$doc->loadHTML("assets.html");
$tables = $doc->getElementsByTagName('table');
foreach($tables as $table) {
$content = $doc->saveHTML($table);
}
echo $content;
逐步调试,$doc->loadHTML("assets.html")
返回true
,DOMNodeList::__set_state(array(
))
获得$tables
。
assets.html的部分如下:
<div class="btn-group">
<button type="button" class="btn btn-info dropdown-toggle" id="manage" data-toggle="dropdown">
新增
<span class="caret"></span>
</button>
<ul class="dropdown-menu " role="menu">
<li><a href="#" id="newassets">增加资产</a></li>
<li><a href="#" id="newdetail">增加明细</a></li>
</ul>
</div>
<button type="button" class="btn btn-danger" id="deleteassets" value="" >删除</button>
<button type="button" class="btn btn-success" id="modifyassets" value="" >修改</button>
<button type="button" class="btn btn-warning" id="lend-lease" value="" >领用</button>
</div>
<div class="dataTable_hr_info">
<table class="table table-striped table-bordered table-hover" action="" id="assetsInfo">
<thead>
<tr>
<th data-field="state" data-checkbox="true"></th>
<th data-field="pr" >序号</th>
<th data-field="category">资产类别</th>
<th data-field="name" >资产名称</th>
<th data-field="model">型号</th>
<th data-field="unit">单位</th>
<th data-field="count">数量</th>
</tr>
</thead>
</table>
</div>
</div>
</div>
答案 0 :(得分:1)
您正在从文件加载HTML,因此请使用:$doc->loadHTMLFile("assets.html");
而不是$doc->loadHTML("assets.html");
之后您应该在$content
内获取数据。
答案 1 :(得分:0)
在您的代码中发现了一些错误。
1:你不能使用loadHTML方法来加载html。
2:在您的HTML文件中,某些代码未正确关闭。
检查以下代码,
<强> PHP 强>
<?php
$dom = new DomDocument();
$dom->loadHTML(file_get_contents('assets.html'));
$tables = $dom->documentElement->getElementsByTagName('table');
foreach($tables as $table) {
$content = $dom->saveHTML($table);
}
echo $content;
?>
<强> HTML 强>
<div class="btn-group">
<button type="button" class="btn btn-info dropdown-toggle" id="manage" data-toggle="dropdown">
新增
<span class="caret"></span>
</button>
<ul class="dropdown-menu " role="menu">
<li><a href="#" id="newassets">增加资产</a></li>
<li><a href="#" id="newdetail">增加明细</a></li>
</ul>
</div>
<button type="button" class="btn btn-danger" id="deleteassets" value="" >删除</button>
<button type="button" class="btn btn-success" id="modifyassets" value="" >修改</button>
<button type="button" class="btn btn-warning" id="lend-lease" value="" >领用</button>
<div class="dataTable_hr_info">
<table class="table table-striped table-bordered table-hover" action="" id="assetsInfo">
<thead>
<tr>
<th data-field="state" data-checkbox="true"></th>
<th data-field="pr" >1</th>
<th data-field="category">2</th>
<th data-field="name" >3</th>
<th data-field="model">4</th>
<th data-field="unit">5</th>
<th data-field="count">6</th>
</tr>
</thead>
</table>
</div>