Recently I stumped upon a handy WordPress function which closes HTML tags.
The function name is force_balance_tags()
.
Taking as an example unclosed list items:
echo force_balance_tags( '<ul>
<li>this
<li>is
<li>a
<li>list
</ul>' );
It will produce something like this:
<ul>
<li>this
</li><li>is
</li><li>a
</li><li>list
</li></ul>