MarksCode MarksCode - 4 months ago 6
PHP Question

Tidy not closing <hr> tag

I'm trying to use the tidy functions to clean up an html string that doesn't have a closing

</hr>
tag:

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


However when I use the following lines:

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


the
</hr>
tag isn't being added, outputting:

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


Is the tidy library just not capable of closing an
<hr>
tag or am I missing something?

Answer

Well, the <hr> (thematic break) tag is not one to be closed.

From W3 -> hr:

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


If you really feel like it, you could do something like:

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

That will pretend the tag is self-closing and prevent SimpleXMLElement from going hysterical about not closing it.