2016-07-27 85 views
2

我试图用整齐的功能,清理不具有关闭</hr>标记的HTML字符串:整洁不关闭<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> 

然而,当我用下面几行:

$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>标签,或者我错过了什么?

+0

你是什么意思你要'


'拥有'',它是一个void元素,它不需要关闭 – Ghost

+0

那么当我尝试从unclosed字符串中创建SimpleXMLElement时,出现错误,但是当它关​​闭时没有错误。这就是为什么我需要它关闭。 – MarksCode

+0

然后使用不太严格的'DOMDocument' – Ghost

回答

1

那么,<hr>(专题打破)标签是不是一个被关闭。

W3C -> hr

hr元素是一个空元素。 hr元素必须具有开始标记,但不得有结束标记


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

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

这将假装的标签自闭和防止SimpleXMLElement从去歇斯底里约不关闭它。

+0

我试图使一个SimpleXMLElement出字符串,所以这就是为什么我需要它关闭。当它关闭时,我不会有任何错误,但是当它打开时我无法做到。 – MarksCode

+0

是的,我可能不得不这样做。我是PHP新手,所以我试图弄清楚如何在标签前插入一个正在结束的标记。 – MarksCode

+0

啊。这似乎与制作SimpleXMLElement一起工作。谢谢。 – MarksCode

相关问题