Tabbed Ouput with Tidy

3:21 am HTML, PHP

In response to this thread at WebDeveloper.com, I came up with the idea of using PHP's Tidy functions to format the HTML output from a script. The basic idea was to capture all the output by using ob_start() to buffer the output and then ob_get_clean() to save it to a variable. Then just run it through the tidy_repair_string() function with a couple configuration settings to indent it.


<?php
ob_start
();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>test</title>
</head>
<body>
<h1>Test</h1>
<ul>
<li>This is a test.</li>
<li>It is only a test.</li>
</ul>
</body>
</html>
<?php
$text
= ob_get_clean();
$config = array(
'indent' => true,
'indent-spaces' => 4
);
$text = tidy_repair_string($text, $config);
echo
$text;

But then I found out that the poster needed the indenting to be by tabs, not spaces; so I modified the code to only indent with 1 space instead of 4, then run a preg_replace() with the "e" modifier executing a call to str_replace() to replace any spaces which follow a newline with tab characters, et voilà!.


<?php
ob_start
();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>test</title>
</head>
<body>
<h1>Test</h1>
<ul>
<li>This is a test.</li>
<li>It is only a test.</li>
</ul>
</body>
</html>
<?php
$text
= ob_get_clean();
$config = array(
'indent' => true,
'indent-spaces' => 1
);
$text = tidy_repair_string($text, $config);
$text = preg_replace('/(?<=\n) +/e', 'str_replace(" ", "\t", "\\0")', $text);
echo
$text;
?>

So if you find the [X]HTML being output by your PHP scripts getting out of control and difficult to read, perhaps you'll find either of these solutions (spaces or tabs) useful as a means of cleaning things up.

The following is the output generated by the second version (note that my source code to HTML converter changed the tabs to non-breaking spaces, but the original did have tabs):


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <title>
            test
        </title>
    </head>
    <body>
        <h1>
            Test
        </h1>
        <ul>
            <li>This is a test.
            </li>
            <li>It is only a test.
            </li>
        </ul>
    </body>
</html>

3 Responses
  1. nrg_alpha :

    Date: September 5, 2008 @ 17:37

    I think you forgot the closing php tag ‘?>’ at the bottom of the first code snippet ;)

    I saved either code snippet (as simple separate php files) and tried running them. I got a white screen (which we know means there’s an issue of some sort).

    While I’m not familiar with any of the tidy functions (as I’ve never used them before), I am rather curious (and I know this doesn’t relate to white screens).

    Is this an extension that needs to be installed separately afterwards or does it already come installed as part of PHP 5.2.6?

    Perhaps I’m simply misunderstanding something.

  2. Charles :

    Date: September 5, 2008 @ 19:07

    Hi, “NRG”.

    Tidy is a PECL extension, so your PHP installation would need to be configured to include it. (I was testing locally on my PC using WAMP, which has a simple PHP extension click-to-install option from the system tray icon.)

    As far as the closing “?>”, it’s optional if it would be the last thing in a file. In fact, I intentionally leave it out of include files that just define functions or classes, as that way I don’t have to worry about accidentally generating output during an include due to a space or newline after the closing “?>”.

  3. nrg_alpha :

    Date: September 5, 2008 @ 20:09

    Ah.. that explains EVERYTHING :) I was wondering if I had to have that extension installed. Funny thing is, whenever I would call a function that belongs to an extension that is not installed, PHP would simply put up an error message stating that that particular function doesn’t exist (or something to that effect).

    I also didn’t realize that you can omit the last ‘?>’ aspect of the PHP tags for the purposes you mention. Nice to know :)

    I definitely see the use for such code snippets.. people hosting blogs / websites on coding tutorials for example would benefit from such a nice and easy formatting system.

    Good topic :)

Leave a Comment

Note: You must be registered and logged in in order to leave a comment.