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 ?
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 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 .

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.