HTML Tidy

function html_tidy( $input_html, $indent = "true", $no_body_tags = "true", $fix = "true" ) {
   ob_start(  );
   $tidy = new tidy;
   $config = array( 'indent' => $indent, 'output-xhtml' => true, 'wrap' => 200, 'clean' => $fix, 'show-body-only' => $no_body_tags );
   $tidy->parseString( $input_html, $config, 'utf8' );
   $tidy->cleanRepair(  );
   $input = $tidy;
   return $input;
}

Comments

  1. User Avatar
    Zeke N
    Permalink to comment#

    So, I’m thinking it would be cool to write the function html_print(); HTML print would basically be: function html_print($html){
    global $doc_html;
    $doc_html .= $html;
    }

    Then, at the end of the page, one could do echo html_tidy($doc_html); This would mean all the php generated html would be formatted nicely. Does that sound like a good idea to anyone else?

  2. User Avatar
    David A.
    Permalink to comment#

    The default function arguments would produce an error since tidy expects a boolean value, but you’re giving a string. The first line should be like so:

    function html_tidy( $input_html, $indent = TRUE,
                                                   $no_body_tags = TRUE, $fix = TRUE ) {

    Other than that, very good suggestion. :-)

  3. User Avatar
    ghindows
    Permalink to comment#

    $function html_tidy( $input_file, $indent = true, $no_body_tags = false, $fix = true ) {
    $tidy = new tidy;
    //config is from http://tidy.sourceforge.net/docs/quickref.html
    $config = array( ‘indent’ => $indent,
    ‘output-html’ => true,
    ‘wrap-attributes’ => false,
    ‘wrap’ => 120,
    ‘show-body-only’ => $no_body_tags
    );
    $tidy->parseFile( $input_file, $config );
    $tidy->cleanRepair( );
    return tidy_get_output($tidy);
    }`

  4. User Avatar
    william
    Permalink to comment#

Submit a Comment

Posting Code

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.

icon-closeicon-emailicon-linkicon-logo-staricon-menuicon-searchicon-staricon-tag