Auto-close HTML tags using PHP
Hi everyone,
Is anyone aware of a function or other bit of code that could quickly and easily close open HTML tags.
We're looking at this for a forum-type situation where members are permitted to use HTML in their messages, however sometimes forget to close the tags and muck up the rest of the page layout. e.g. <b>Hi, I forgot to close the bold tag...doh!
Any ideas greatly appreciated, I've had a hunt around but haven't found anything yet - still searching. :)
Re: Auto-close HTML tags using PHP
That's one of the reasons why BB code was invented.
If you are bent on allowing HTML, one thing you could do is parse the message into a DOM tree (using the DOMDocument class) and see if it is well-formed; if it isn't then you can show the user a message and not submit the post.
However, you really shouldn't let users post raw HTML anyway.
Re: Auto-close HTML tags using PHP
Yeah I know, it's not my forum or code - was just trying to find a solution to a problem they're having. But I think the route will be BB code as you mentioned...
Re: Auto-close HTML tags using PHP
Use PHP 5 and use the loadHTML() method of the DOMDocument object. This auto parses badly formed HTML and a subsequent call to saveXML() will output the valid HTML. The DOM functions can be found here:
http://www.php.net/dom