PHP DOM - 将 THEAD 中的 TD 标签替换为 TH 标签
PHP DOM - Replacing TD tags in a THEAD to TH tags
我正在尝试将 THEAD 中的所有 TD 标签替换为 TH 标签。
我认为最好使用 PHP DOM 扩展名。我是新手,所以我为我缺乏知识而道歉。
我进行了一些搜索,找到了如何替换标签名称。但是,我无法弄清楚如何只替换父级中的标签名称(在本例中为 THEAD 标签)。我想将 TD 保留在 TBODY 中。
这是我的代码,用于缩小 THEAD 中的 TD。这就是我迷路的地方。
如何将 THEAD 中的标签名称更改为 TH?
$html = '<table>
<thead>
<tr>
<td>Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
</tr>
</thead>
<tbody>
<tr>
<td>Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
</tr>
<tr>
<td>Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
</tr>
<tr>
<td>Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
</tr>
</tbody>
</table>';
// create empty document
$document = new DOMDocument();
// load html
$document->loadHTML(html);
// Get theads
$theads = $document->getElementsByTagName('thead');
// Loop through theads (incase there are more than one!)
for($i=0;$i<$theads->length;$i++) {
$thead = $theads->item($i);
// Loop through TR
foreach ($thead->childNodes AS $tr) {
if ($tr->nodeName == 'tr') {
// Loop through TD
foreach ($tr->childNodes AS $td) {
if ($td->nodeName == 'td') {
// Replace this tag
}
}
}
}
}
如果您已查看手册,可以使用此 ->replaceChild()
方法将 td
替换为 th
标签:
$html = '<table>
<thead>
<tr>
<td>Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
</tr>
</thead>
<tbody>
<tr>
<td>Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
</tr>
<tr>
<td>Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
</tr>
<tr>
<td>Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
</tr>
</tbody>
</table>';
// create empty document
$document = new DOMDocument();
// load html
$document->loadHTML($html);
// Get theads
$theads = $document->getElementsByTagName('thead')->item(0); // get thead tag
foreach($theads->childNodes as $tr) { // loop thead rows `tr`
$tds = $tr->getElementsByTagName('td'); // get tds inside trs
$i = $tds->length - 1;
while($i > -1) {
$td = $tds->item($i); // td
$text = $td->nodeValue; // text node
$th = $document->createElement('th', $text); // th element with td node value
$td->parentNode->replaceChild($th, $td); // replace
$i--;
}
}
echo $document->saveHTML();
我正在尝试将 THEAD 中的所有 TD 标签替换为 TH 标签。
我认为最好使用 PHP DOM 扩展名。我是新手,所以我为我缺乏知识而道歉。
我进行了一些搜索,找到了如何替换标签名称。但是,我无法弄清楚如何只替换父级中的标签名称(在本例中为 THEAD 标签)。我想将 TD 保留在 TBODY 中。
这是我的代码,用于缩小 THEAD 中的 TD。这就是我迷路的地方。
如何将 THEAD 中的标签名称更改为 TH?
$html = '<table>
<thead>
<tr>
<td>Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
</tr>
</thead>
<tbody>
<tr>
<td>Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
</tr>
<tr>
<td>Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
</tr>
<tr>
<td>Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
</tr>
</tbody>
</table>';
// create empty document
$document = new DOMDocument();
// load html
$document->loadHTML(html);
// Get theads
$theads = $document->getElementsByTagName('thead');
// Loop through theads (incase there are more than one!)
for($i=0;$i<$theads->length;$i++) {
$thead = $theads->item($i);
// Loop through TR
foreach ($thead->childNodes AS $tr) {
if ($tr->nodeName == 'tr') {
// Loop through TD
foreach ($tr->childNodes AS $td) {
if ($td->nodeName == 'td') {
// Replace this tag
}
}
}
}
}
如果您已查看手册,可以使用此 ->replaceChild()
方法将 td
替换为 th
标签:
$html = '<table>
<thead>
<tr>
<td>Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
</tr>
</thead>
<tbody>
<tr>
<td>Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
</tr>
<tr>
<td>Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
</tr>
<tr>
<td>Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
</tr>
</tbody>
</table>';
// create empty document
$document = new DOMDocument();
// load html
$document->loadHTML($html);
// Get theads
$theads = $document->getElementsByTagName('thead')->item(0); // get thead tag
foreach($theads->childNodes as $tr) { // loop thead rows `tr`
$tds = $tr->getElementsByTagName('td'); // get tds inside trs
$i = $tds->length - 1;
while($i > -1) {
$td = $tds->item($i); // td
$text = $td->nodeValue; // text node
$th = $document->createElement('th', $text); // th element with td node value
$td->parentNode->replaceChild($th, $td); // replace
$i--;
}
}
echo $document->saveHTML();