php - 使用 DOMDocument 将表的内容添加到数组中
问题描述
我有$html
:
$html = '
<table id="myTable">
<tbody>
<tr>
<td>08/20/18</td>
<td> <a href="https://example.com/1a">Text 1 A</a> </td>
<td> <a href="https://example.com/1b">Test 1 B</a> </td>
</tr>
<tr>
<td>08/21/18</td>
<td> <a href="https://example.com/2a">Text 2 A</a> </td>
<td> <a href="https://example.com/2b">Test 2 B</a> </td>
</tr>
</tbody>
</table>
';
使用DOMDocument,我想将表的内容添加到多维中$array
:
$array = array(
// tr 1
array(
array(
'content' => '08/20/18'
),
array(
'content' => 'Text 1 A',
'href' => 'https://example.com/1a'
),
array(
'content' => 'Text 1 B',
'href' => 'https://example.com/1b'
)
),
// tr 2
array(
array(
'content' => '08/21/18'
),
array(
'content' => 'Text 2 A',
'href' => 'https://example.com/1a'
),
array(
'content' => 'Text 2 B',
'href' => 'https://example.com/1b'
)
)
);
到目前为止我尝试过的
我设法获得了table
using的内容xpath
:
// setup DOMDocument
$doc = new DOMDocument();
$doc->loadHTML('<?xml encoding="utf-8" ?>' . $html);
$xpath = new DOMXPath($doc);
// target table using xpath
$results = $xpath->query("//*[@id='myTable']");
if ($results->length > 0) {
var_dump($results->item(0));
var_dump($results->item(0)->nodeValue);
}
测试一下。将每个内容放入的方法是tr
什么$array
?
解决方案
<?php
$html = '
<table id="myTable">
<tbody>
<tr>
<td>08/20/18</td>
<td> <a href="https://example.com/1a">Text 1 A</a> </td>
<td> <a href="https://example.com/1b">Test 1 B</a> </td>
</tr>
<tr>
<td>08/21/18</td>
<td> <a href="https://example.com/2a">Text 2 A</a> </td>
<td> <a href="https://example.com/2b">Test 2 B</a> </td>
</tr>
</tbody>
</table>
';
$data = [];
$doc = new DOMDocument();
$doc->loadHTML('<?xml encoding="utf-8" ?>' . $html);
$xpath = new DOMXPath($doc);
$trs = $xpath->query("//*[@id='myTable']/tbody/tr");
foreach ($trs as $i => $tr) {
/** @var DOMElement $td */
foreach ($tr->childNodes as $td) {
if ($td instanceof DOMElement) {
/** @var DOMElement $a */
$row = [];
foreach ($td->childNodes as $a) {
/** @var DOMAttr $attribute */
$row['content'] = $td->nodeValue;
if ($a->hasAttributes()) {
foreach ($a->attributes as $attribute) {
$row[$attribute->name] = $attribute->value;
}
}
}
$data[$i][] = $row;
}
}
}
var_dump($data);
推荐阅读
- java - mysql,Springboot 错误 - 无法打开 JDBC 连接以执行 DDL
- c - 如何删除特定行并替换它
- javascript - 如何检查浏览器中实现了现代 javascript(ES2015++、ES2018、ES2019)的哪些功能?
- javascript - 让 javascript 函数将对自身的引用传递给另一个函数
- reactjs - React app - 在没有服务器的情况下上传 JSON 文件并将其读取到变量中?
- azure-hdinsight - 从 Azure 门户创建 HDInsight 群集时使用 SAS 密钥附加其他存储帐户
- python - 重塑张量是否保留了原始张量的特征?
- c++ - 在 C++ 中链接 MKL 库
- vue.js - 如何在 vue composition api 中使用激活和停用?
- python-3.x - Jupyter Notebook 没有输出。未显示错误