我正在尝试构建一个函数,它可以清除 HTML 文档中包含空内容或没有属性的所有元素,并且我想要 igonre<br>
和<hr>
标签。
这是我当前的代码。
<?php
function clean($html){
$dom = new DOMDocument();
$dom->loadHTML($html)
$xpath = new DOMXPath($dom);
while (($node_list = $xpath->query('//*[not(*) and not(@*) and not(text()[normalize-space()])]')) && $node_list->length) {
foreach ($node_list as $node) {
$node->parentNode->removeChild($node);
}
}
return $dom->saveHTML();
}
我如何选择忽略br
和hr
使用 xpath 查询的所有节点。
示例输入示例:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<p><strong><span style="font-size:16px;">Specifications:</span></strong><br>
<span style="font-size:16px;">1. Material: Polymer</span><br>
<span style="font-size:16px;">2. Weight: 7.94oz / 225g</span><br>
<span style="font-size:16px;">3. Color: <strong><span style="color:#ff0000;background-color:#ffffff;">Black / White / Blue /</span></strong> <strong><span style="color:#ff0000;background-color:#ffffff;">Red</span></strong></span><br>
<span style="font-size:16px;">4. Dimensions: (7.48 x 7.87 x 3.35)" / (19 x 20 x 8.5)cm (L x W x H)</span><br>
<span style="font-size:16px;">5. Driver Unit: 2 Speakers(57mm)</span><br>
<span style="font-size:16px;">6. Transducer Type: Dynamic</span><br>
<span style="font-size:16px;">7. Bluetooth Version: V4.1</span><br>
<span style="font-size:16px;">8. Operating Distance: up to 10m(Free Space)</span><br>
<span style="font-size:16px;">9. Profiles: A2DP,AVRCP,HSP,HFP</span><br>
<span style="font-size:16px;">10. Speaker Impendence: 16Ω</span><br>
<span style="font-size:16px;">11. Frequency Response: 20Hz-20KHz</span><br>
<span style="font-size:16px;">12. Sensitivity: 110dB</span><br>
<span style="font-size:16px;">13. THD: <0.1%</span><br>
<span style="font-size:16px;">14. <span style="color:#6600ff;">Support Micro SD Card Capacity</span>: up to 32GB</span><br>
<span style="font-size:16px;">15. Micro SD Card Play Time: about 15 hours</span><br>
<span style="font-size:16px;">16. FM Play Time: about 15 hours</span><br>
<span style="font-size:16px;">17. Bluetooth Music Time: about 40 hours</span><br>
<span style="font-size:16px;">18. Talk Time: about 45 hours</span><br>
<span style="font-size:16px;">19. Standby Time: 1620 hours(around 50Days)</span><br>
<span style="font-size:16px;">20. Fully Charged Time: about 2 hours</span><br>
<span style="font-size:16px;">21. Operating Environment: -10~50℃</span></p><span style="font-size:16px;"></span>
<ul>
<li><span style="font-size:16px;"></span></li>
<li><span style="font-size:16px;"></span></li>
<li><span style="font-size:16px;"></span></li>
</ul>
<p></p>
</body>
</html>
由于某些没有人知道的原因,这部分没有内容:
我想删除它,同时保留断线。
当前输出示例:
<html>
<head>
<title></title>
</head>
<body>
<p><strong><span style="font-size:16px;">Specifications:</span></strong> <span style="font-size:16px;">1. Material: Polymer</span> <span style="font-size:16px;">2. Weight: 7.94oz / 225g</span> <span style="font-size:16px;">3. Color: <strong><span style="color:#ff0000;background-color:#ffffff;">Black / White / Blue /</span></strong> <strong><span style="color:#ff0000;background-color:#ffffff;">Red</span></strong></span> <span style="font-size:16px;">4. Dimensions: (7.48 x 7.87 x 3.35)" / (19 x 20 x 8.5)cm (L x W x H)</span> <span style="font-size:16px;">5. Driver Unit: 2 Speakers(57mm)</span> <span style="font-size:16px;">6. Transducer Type: Dynamic</span> <span style="font-size:16px;">7. Bluetooth Version: V4.1</span> <span style="font-size:16px;">8. Operating Distance: up to 10m(Free Space)</span> <span style="font-size:16px;">9. Profiles: A2DP,AVRCP,HSP,HFP</span> <span style="font-size:16px;">10. Speaker Impendence: 16Ω</span> <span style="font-size:16px;">11. Frequency Response: 20Hz-20KHz</span> <span style="font-size:16px;">12. Sensitivity: 110dB</span> <span style="font-size:16px;">13. THD: <0.1%</span> <span style="font-size:16px;">14. <span style="color:#6600ff;">Support Micro SD Card Capacity</span>: up to 32GB</span><span style="font-size:16px;">15. Micro SD Card Play Time: about 15 hours</span> <span style="font-size:16px;">16. FM Play Time: about 15 hours</span> <span style="font-size:16px;">17. Bluetooth Music Time: about 40 hours</span> <span style="font-size:16px;">18. Talk Time: about 45 hours</span><span style="font-size:16px;">19. Standby Time: 1620 hours(around 50Days)</span><span style="font-size:16px;">20. Fully Charged Time: about 2 hours</span><span style="font-size:16px;">21. Operating Environment: -10~50℃</span></p>
</body>
</html>
所需的输出示例:
<html>
<head>
<title></title>
</head>
<body>
<p><strong><span style="font-size:16px;">Specifications:</span></strong><br>
<span style="font-size:16px;">1. Material: Polymer</span><br>
<span style="font-size:16px;">2. Weight: 7.94oz / 225g</span><br>
<span style="font-size:16px;">3. Color: <strong><span style="color:#ff0000;background-color:#ffffff;">Black / White / Blue /</span></strong> <strong><span style="color:#ff0000;background-color:#ffffff;">Red</span></strong></span><br>
<span style="font-size:16px;">4. Dimensions: (7.48 x 7.87 x 3.35)" / (19 x 20 x 8.5)cm (L x W x H)</span><br>
<span style="font-size:16px;">5. Driver Unit: 2 Speakers(57mm)</span><br>
<span style="font-size:16px;">6. Transducer Type: Dynamic</span><br>
<span style="font-size:16px;">7. Bluetooth Version: V4.1</span><br>
<span style="font-size:16px;">8. Operating Distance: up to 10m(Free Space)</span><br>
<span style="font-size:16px;">9. Profiles: A2DP,AVRCP,HSP,HFP</span><br>
<span style="font-size:16px;">10. Speaker Impendence: 16Ω</span><br>
<span style="font-size:16px;">11. Frequency Response: 20Hz-20KHz</span><br>
<span style="font-size:16px;">12. Sensitivity: 110dB</span><br>
<span style="font-size:16px;">13. THD: <0.1%</span><br>
<span style="font-size:16px;">14. <span style="color:#6600ff;">Support Micro SD Card Capacity</span>: up to 32GB</span><br>
<span style="font-size:16px;">15. Micro SD Card Play Time: about 15 hours</span><br>
<span style="font-size:16px;">16. FM Play Time: about 15 hours</span><br>
<span style="font-size:16px;">17. Bluetooth Music Time: about 40 hours</span><br>
<span style="font-size:16px;">18. Talk Time: about 45 hours</span><br>
<span style="font-size:16px;">19. Standby Time: 1620 hours(around 50Days)</span><br>
<span style="font-size:16px;">20. Fully Charged Time: about 2 hours</span><br>
<span style="font-size:16px;">21. Operating Environment: -10~50℃</span></p>
</body>
</html>
先感谢您。