While it's messy as hell, as an interim solution, write a subroutine that goes though it character by character which records the following things:
total characters so far
if you are in a tag (i.e. started with a <)
what the last character was
how many tags you are in
how many open tags there are (img could be problematic with this. ditto on any other atomic tags. xhtml would be easier)
what the open tags are and what order they are in
from that you should be able to run through the characters, taking note of all the tags that you'll need to close at the end.