Rotates.org

March 27, 2009 - Ooh, pretty

Just thought I’d share a little trick I’ve just discovered while anally going through a couple of my projects making all the HTML properly indented… it seems I spend a lot of my time trying to find ways to make my life in some way easier, and this is one of the ones which worked!

ob_start();

// your code here

$pretty = new DOMDocument("1.0");
$pretty->preserveWhiteSpace = false;
$pretty->loadXML(ob_get_clean());
$pretty->formatOutput = true;
echo $pretty->saveXML();

Essentially it formats your XHTML (you’d have to use Tidy for regular HTML, but we don’t like that dirty stuff) correctly via PHP’s own XML and DOM manipulation classes. Very nifty – though of course it adds a bit of overhead, and makes everything much less forgiving as far as not well-formedness goes, but hey – I consider that an incentive to write better code 🙂

0 | PHP

Comments are closed.