gpt4 book ai didi

php - 整洁不关闭
标签

转载 作者:行者123 更新时间:2023-11-28 01:08:36 25 4
gpt4 key购买 nike

我正在尝试使用 tidy 函数来清理没有结尾 </hr> 的 html 字符串标签:

<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>

但是当我使用以下行时:

$tidy = tidy_parse_string($data);
tidy_clean_repair($tidy);
echo ($tidy);

</hr>未添加标签,输出:

<html>
<head>
<title>301 Moved Permanently</title>
</head>
<body bgcolor='white'>
<center>
<h1>301 Moved Permanently</h1>
</center>
<hr>
<center>nginx</center>
</body>
</html>

整洁的库是否无法关闭 <hr>标记还是我遗漏了什么?

最佳答案

嗯,<hr> (主题中断)标签不是要关闭的标签。

来自 W3C -> hr :

The hr element is a void element. An hr element must have a start tag but must not have an end tag.


如果你真的需要,你可以这样做:

$html = str_replace('<hr>', '<hr/>', $html);

这将假装标签自关闭并阻止SimpleXMLElement不要因为不关闭它而歇斯底里。

关于php - 整洁不关闭 <hr> 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38624680/

25 4 0