Link to home
Start Free TrialLog in
Avatar of MadDog986
MadDog986

asked on

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.

Example:

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

to:

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

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
     ...
else
     ...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;"
ASKER CERTIFIED SOLUTION
Avatar of alorentz
alorentz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 -->"

or:
strString = "<table><tr><td>table contents</td></tr><!-- comment --></table>"
Avatar of MadDog986

ASKER

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...
Yes i am still looking for the code...
What haven't you understood?  This basically is not possible...
"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.
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.
No once has provided the answer i am looking for. Please just delete this topic.
I DID NOT ACCEPT THIS ANSWER AND REQUESTED IT BE DELETED!!!!!!!!!!!!!!!!!!!!!
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.