从 HTML 中删除带有换行符的空标签
Remove empty tags with line breaks from HTML
我有以下 HTML:
<body>Summary: <br>
<table class="stats data tablesorter marg-bottom">
<thead><tr><th>Team</th><th>Wins</th><th>Losses</th><th>Ties</th><th>Win %</th></tr></thead>
<tbody>
<tr>
<td>Team 1</td>
<td>95</td>
<td>74</td>
<td>0</td>
<td>56.21</td>
</tr>
<tr>
<td>Team 2</td>
<td>74</td>
<td>95</td>
<td>0</td>
<td>43.79</td>
</tr>
</tbody>
</table>
<div>
</div>
</body>
我想要这样的结果:
<body>Summary: <br>
<table class="stats data tablesorter marg-bottom">
<thead><tr><th>Team</th><th>Wins</th><th>Losses</th><th>Ties</th><th>Win %</th></tr></thead>
<tbody>
<tr>
<td>Team 1</td>
<td>95</td>
<td>74</td>
<td>0</td>
<td>56.21</td>
</tr>
<tr>
<td>Team 2</td>
<td>74</td>
<td>95</td>
<td>0</td>
<td>43.79</td>
</tr>
</tbody>
</table>
</body>
最简单的方法是正确编码,不幸的是,它来自一个非常非常旧的 CKEditor 版本,我无法升级它(由于其他影响)。
什么 preg_replace
或递归函数或循环可以 运行 删除空的 <div>
标签和不需要的空行?
假设你在一个名为 $html
的变量中有这个 HTML:
// Replace empty <div> tags with nothing
$html = preg_replace("/<div>\s*<\/div>/", "", $html);
// Replace multiple newlines in a row with a single newline
$html = preg_replace("/\n+/", "\n", $html);
echo $html;
编辑
完整的工作代码,包括输出:
<?php
$html = <<<END
<body>Summary: <br>
<table class="stats data tablesorter marg-bottom">
<thead><tr><th>Team</th><th>Wins</th><th>Losses</th><th>Ties</th><th>Win %</th></tr></thead>
<tbody>
<tr>
<td>Team 1</td>
<td>95</td>
<td>74</td>
<td>0</td>
<td>56.21</td>
</tr>
<tr>
<td>Team 2</td>
<td>74</td>
<td>95</td>
<td>0</td>
<td>43.79</td>
</tr>
</tbody>
</table>
<div>
</div>
</body>
END;
// Replace empty <div> tags with nothing
$html = preg_replace("/<div>\s*<\/div>/", "", $html);
// Replace multiple newlines in a row with a single newline
$html = preg_replace("/\n+/", "\n", $html);
echo $html;
// OUTPUT:
// <body>Summary: <br>
// <table class="stats data tablesorter marg-bottom">
// <thead><tr><th>Team</th><th>Wins</th><th>Losses</th><th>Ties</th><th>Win %</th></tr></thead>
// <tbody>
// <tr>
// <td>Team 1</td>
// <td>95</td>
// <td>74</td>
// <td>0</td>
// <td>56.21</td>
// </tr>
// <tr>
// <td>Team 2</td>
// <td>74</td>
// <td>95</td>
// <td>0</td>
// <td>43.79</td>
// </tr>
// </tbody>
// </table>
// </body>
?>
我有以下 HTML:
<body>Summary: <br>
<table class="stats data tablesorter marg-bottom">
<thead><tr><th>Team</th><th>Wins</th><th>Losses</th><th>Ties</th><th>Win %</th></tr></thead>
<tbody>
<tr>
<td>Team 1</td>
<td>95</td>
<td>74</td>
<td>0</td>
<td>56.21</td>
</tr>
<tr>
<td>Team 2</td>
<td>74</td>
<td>95</td>
<td>0</td>
<td>43.79</td>
</tr>
</tbody>
</table>
<div>
</div>
</body>
我想要这样的结果:
<body>Summary: <br>
<table class="stats data tablesorter marg-bottom">
<thead><tr><th>Team</th><th>Wins</th><th>Losses</th><th>Ties</th><th>Win %</th></tr></thead>
<tbody>
<tr>
<td>Team 1</td>
<td>95</td>
<td>74</td>
<td>0</td>
<td>56.21</td>
</tr>
<tr>
<td>Team 2</td>
<td>74</td>
<td>95</td>
<td>0</td>
<td>43.79</td>
</tr>
</tbody>
</table>
</body>
最简单的方法是正确编码,不幸的是,它来自一个非常非常旧的 CKEditor 版本,我无法升级它(由于其他影响)。
什么 preg_replace
或递归函数或循环可以 运行 删除空的 <div>
标签和不需要的空行?
假设你在一个名为 $html
的变量中有这个 HTML:
// Replace empty <div> tags with nothing
$html = preg_replace("/<div>\s*<\/div>/", "", $html);
// Replace multiple newlines in a row with a single newline
$html = preg_replace("/\n+/", "\n", $html);
echo $html;
编辑
完整的工作代码,包括输出:
<?php
$html = <<<END
<body>Summary: <br>
<table class="stats data tablesorter marg-bottom">
<thead><tr><th>Team</th><th>Wins</th><th>Losses</th><th>Ties</th><th>Win %</th></tr></thead>
<tbody>
<tr>
<td>Team 1</td>
<td>95</td>
<td>74</td>
<td>0</td>
<td>56.21</td>
</tr>
<tr>
<td>Team 2</td>
<td>74</td>
<td>95</td>
<td>0</td>
<td>43.79</td>
</tr>
</tbody>
</table>
<div>
</div>
</body>
END;
// Replace empty <div> tags with nothing
$html = preg_replace("/<div>\s*<\/div>/", "", $html);
// Replace multiple newlines in a row with a single newline
$html = preg_replace("/\n+/", "\n", $html);
echo $html;
// OUTPUT:
// <body>Summary: <br>
// <table class="stats data tablesorter marg-bottom">
// <thead><tr><th>Team</th><th>Wins</th><th>Losses</th><th>Ties</th><th>Win %</th></tr></thead>
// <tbody>
// <tr>
// <td>Team 1</td>
// <td>95</td>
// <td>74</td>
// <td>0</td>
// <td>56.21</td>
// </tr>
// <tr>
// <td>Team 2</td>
// <td>74</td>
// <td>95</td>
// <td>0</td>
// <td>43.79</td>
// </tr>
// </tbody>
// </table>
// </body>
?>