In this new tutorial from DevShed, they demonstrate the use of a handy little bit of functionality to help keep your code (and markup) clean - Tidy.
Now that you know that the Tidy (X)HTML formatting/correcting application can be called directly from your own PHP 5 scripts, over the course of this series, which is comprised of three friendly tutorials, I'm going to walk you through using the bunch of useful functions included with this library.
This tutorial (part one) talks about the parsing of (X)HTML strings and using the tidy_clear_repair, tidy_parse_file and tidy_repair_file functions to handle the cleanup of (X)HTML strings and files automatically.