Results 1 to 4 of 4

Thread: Quick and dirty way to produce valid HTML markup?

  1. #1
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default Quick and dirty way to produce valid HTML markup?

    What do folks think about this method of taking an invalid string of HTML and making it into a valid one?

    PHP Code:
    <?php
    $dirty 
    "<div>some content</div></div></p><img src='bob.jpg'>
    <div>Hello!</div></p><p></div>"
    ;
    $x = new DOMDocument;
    @
    $x->loadHTML($dirty);
    $clean = @$x->saveHTML();
    $str preg_replace('/^.*<body>|<\/body>.*$/m'''$clean);
    echo 
    implode("\n"array_slice(explode("\n"$str), 2));
    ?>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  2. #2
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

    John, should this work when I put it inside a php-file? I can't get it to work.

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    PHP 5+ required. I've made some refinements** since, but yes, as long as PHP's DOMDocument class is supported, it should more or less work.

    **said refinements (most of them at least*):

    Code:
    <?php
    header('Content-Type: text/html; charset=utf-8'); // most often wise - adjust if necessary
    $dirty = "<div>some content©</div></div></p><img src='bob.jpg'>
    <div>Hello! €</div></p><p></div>";
    $x = new DOMDocument;
    @$x->loadHTML($dirty);
    $clean = $x->saveHTML();
    $str = implode("\n", array_slice(explode("\n", $clean), 1));
    echo trim(preg_replace('/^.*<body>|<\/body>.*$/m', '', $str));
    ?>
    *some refinement may be required for any particular purpose. If tidy:

    http://php.net/manual/en/class.tidy.php

    is available, use that. I'm interested in this approach (the one without tidy) because I'm working on code that already requires PHP 5.2 and that often will not be run in environments where tidy is available.

    Demo of the code in this post:

    http://john.dynamicdrive.com/demos/tidbits/fix_html.php

    Use the browser's view source and compare to the $dirty string.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

    Quote Originally Posted by jscheuer1 View Post
    PHP 5+ required.
    Thanks.

Similar Threads

  1. Replies: 1
    Last Post: 01-14-2013, 08:58 PM
  2. Replies: 4
    Last Post: 02-25-2011, 02:41 PM
  3. Dynamic Ajax Content: Specifying DIV ID of target page in HTML markup?
    By Sputnik in forum Dynamic Drive scripts help
    Replies: 2
    Last Post: 02-22-2011, 07:08 PM
  4. Dirty-mouthed fox
    By techno_race in forum Computer hardware and software
    Replies: 4
    Last Post: 08-07-2010, 05:18 AM
  5. Quick HTML coding question
    By rollings in forum HTML
    Replies: 1
    Last Post: 07-25-2007, 08:34 PM

Bookmarks

Posting Permissions

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