in reply to Ensuring HTML is "balanced"

You can try adapting Re: Pair Tag missing.