I am posting a form with a textarea in it.
I allow HTML to be posted.
Now I wish to check if the user has closed the tags he has put in the html he posted… when I am displaying that HTML, the broken tags like divs and tables etc spoil the whole page display… any way to check for proper tag useage in php or javascript ?
Any pointers or help would be appreciated.
Thanks
You might want to take a look at a PHP tool called HTMLPurifier — there is a demo page available, if you want to quickly check what it can do.
It takes “sort of” HTML as input, and gives well-formed HTML as ouput ; this way, you are not forcing your users to input well-formed HTML, but you can “correct” what they typed.
Another nice thing is, you can specify which tags and attributes are allowed ; which is good for security too :
<p>and<strong>tags, but not<script>.<a>+href; but not<a>+onclickFor instance, here is some not-well-formed HTML you can give to it :
And here is the well-formed / secured HTML given as output :
What has changed ?
<strong>tag in the first paragraph has been automatically closed<script>tag and its content have been removed<em>and<strong>tags in the second paragraph has been corrected.This was just a quick example, of course — I hope it helped.