Close all open HTML tags

Im trying to find a function that will close all open HTML tags in a  string to prevent people from cheating a WYSIWYG form.


strString = "<table><tr><td>table contents</td></tr><!-- comment"


strString = "<table><tr><td>table contents</td></tr></table><!-- comment -->"
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You posted in asp so I assume you are looking for server code that performs this function once the form is POSTed (this could also be done in javascript on the client). Your specific example would be simple to check for:

lastLT = instrrev(strString,"<")
lastGT = instrrev(strString,">")

if lastLT>0 and lastGT>lastLT then
     ...code to close/remove tag
end if
another way is not to allow HTML tags in the form results. That can be done by replacing "<" with "&lt;" and ">" with "&gt;"
>>Im trying to find a function that will close all open HTML tags in

I don't think that close ANY HTML tag?  Sounds remotely impossible...of course not totally impossible, but remotely :)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

i didn't really address how to close it because I think you are better off not allowing HTML in the first place. If you are simply trying to close a trailing open HTML tag as in your example, your easiest way to close is is to append ">" to the string when you find an open tag (as I showed above). This may not result in well formed HTML.

The problem is complicated, as alorentz states, if it needs to be well formed. Further complicated if you want to identify open html tags anywhere in the string (i.e. "<span hello world!</span>"). The latter will take a lot of (tons) coding.
you also would need to be aware of nesting.  which tags need to be closed before others.  you would have to give a heirarchy of which tags need to be closed before others.

strString = "<table><tr><td>table contents</td></tr><!-- comment"

could be:
strString = "<table><tr><td>table contents</td></tr></table><!-- comment -->"

strString = "<table><tr><td>table contents</td></tr><!-- comment --></table>"
MadDog986Author Commented:
Im still looking for the solution and its not disabling all HTML.
honestly, you are fighting an uphill battle.  dreamweaver doesnt even close tags that are left open and thats a $500 software package.  it doesnt always even recognize the correct tags.  
>>Im still looking for the solution

I will bet $500 that the solution (something that corrects all HTML) will not be found here <grin>

If we could all have magic code fixers, there wouldn't be any reason for us at all.
Do you need more assistance?  If not please close...
MadDog986Author Commented:
Yes i am still looking for the code...
What haven't you understood?  This basically is not possible...
MadDog986Author Commented:
"This basically is not possible..."

Not with an attitude like that.

Still waiting for the answer and i will accept the answer when posted.
Heh, heh...good luck dude.  Hope you don't have a deadline.
MadDog986Author Commented:
I have not abandoned this topic, still waiting for an answer...
Sometimes the correct answer is "No".  There currently is no software that does this.
MadDog986Author Commented:
No once has provided the answer i am looking for. Please just delete this topic.
MadDog986Author Commented:
Don't get "mad", MADDOG.  Again, sometimes the correct answer is NO.  There is NOTHING that could do this effectively.

And, I put myself on the line by saying NOTHING...because that's pretty definite, but I'm quite sure this cannot be done, and there is no tool that does it.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.