MarksCode MarksCode - 1 year ago 79
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


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

However when I use the following lines:

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

tag isn't being added, outputting:

<title>301 Moved Permanently</title>
<body bgcolor='white'>
<h1>301 Moved Permanently</h1>

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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download