Results 1 to 4 of 4

Thread: Auto-close HTML tags using PHP

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2004
    Posts
    98

    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.

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2004
    Posts
    98

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

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    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
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width