April 3, 2012

Closing HTML Tags Automatically

Question by user870283

I’ve allowed users to use <pre></pre> tags to display code in comments and articles, but I’ve come across a problem that I’m struggling to solve. When a user fails to close an HTML tag, for example:

    <pre>
        <html>
            <head>

            </head>
    </pre>

the comment appears to be blank. What I’m looking for is some sort of function that will automatically close any HTML tags that the user missed.

Thanks in advance.

Answer by cha55son

Well it’s going to get nasty if you dont use a framework but your courage is admired. Hopefully this will be a nudge in the right direction.

The simplest, non-framework solution I can think of is using a stack to push and pop tags while parsing the string from the user.

pseudo code

userData = getUserData();
stack = array();
loop (line in userData) {
   matches = search for "<*>"; // may have multiple on one line
   loop (match in matches) {
      tagName = getTagNameFrom(match);
      if ("/" is not found) {
         push tagName on stack;
      } else if ("/" is found) {
         pop tagName off stack; 
         // There was an error if the stack is
         // empty or the tagName that was popped was not
         // the same.
      }
   }
}

This is by no means comprehensive and a framework is really recommended here, but hopefully it can help out a bit.

Answer by Starx

You can use HTML Tidy to solve this problem. To finds and closes the unclosed tags, automatically.

Project Page

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!