Jump to content

how I can correct posted html to a html that is safe ?


alexmadsen

Recommended Posts

HI ,

whenever my site users posted a text(html base without strip tags) , if html post contain a closed tags and incomplete tags ; mixed my site main html .
example :
posted :

&nbsp;11111</TR><TR>
<DIV id=postbody>
<P>&nbsp;</P>
<P>2222<BR>3333 </TR><TR>&nbsp;<BR>44444<BR>- &nbsp;<BR>-55555<BR>- 666666666666666666&nbs;pgsdg<BR>- 77777777777&nbsp; ... sdgsdsdg <BR>- 88888888888888 </P></DIV>

********
corrected with gmail editor :

&nbsp;11111<table><tr>
<td><div>
<p>&nbsp;</p>
<p>2222
3333 </p></div></td></tr><tr><td><img>&nbsp;
44444
- <img>&nbsp;<img>

-55555
- 666666666666666666<img>&amp;nbs;pgsdg
- 77777777777<img>&nbsp; ... sdgsdsdg <img>


- 88888888888888 <img></td></tr></table>

</div>
************************************
how I can correct posted html to a html that is safe to my main html similar gmail? with php or javascript ?
please help me ?
Link to comment
Share on other sites

There is a very easy way to do this. Firefox and IE have this cool function that will correct any HTML code that is inserted into the page using JavaScript. They'll close all the tags and fix the HTML code without you having to do anything. Here is a basic script that will take the input from a textarea and assign it to a div. The browser will fix the HTML. Finally, get the HTML value for that div and assign it the textarea value.
[code]
<script language="javascript">
function correctHTML(){
txt = document.getElementById('HTML_Code').value;
document.getElementById('fixing_div').innerHTML = txt;
document.getElementById('HTML_Code').value = document.getElementById('fixing_div').innerHTML;
}
</script>
<textarea name="HTML_Code" id="HTML_Code"></textarea>
<div style="display:none;" id="fixing_div"></div>
<button onclick="correctHTML();">Correct HTML</button>
[/code]
Link to comment
Share on other sites

[quote author=nogray link=topic=123345.msg510670#msg510670 date=1169488203]
There is a very easy way to do this. Firefox and IE have this cool function that will correct any HTML code that is inserted into the page using JavaScript. They'll close all the tags and fix the HTML code without you having to do anything. Here is a basic script that will take the input from a textarea and assign it to a div. The browser will fix the HTML. Finally, get the HTML value for that div and assign it the textarea value.
[code]
<script language="javascript">
function correctHTML(){
txt = document.getElementById('HTML_Code').value;
document.getElementById('fixing_div').innerHTML = txt;
document.getElementById('HTML_Code').value = document.getElementById('fixing_div').innerHTML;
}
</script>
<textarea name="HTML_Code" id="HTML_Code"></textarea>
<div style="display:none;" id="fixing_div"></div>
<button onclick="correctHTML();">Correct HTML</button>
[/code]
[/quote]

this is not work correctly in IE with </tr> or <tr> tags .
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.