I recently looked at my source code and it was a real mess.
my php source:
echo \'Rar\'; echo \'Rar\'; e
You can set up and output buffer and then run the buffer through htmltidy. The tidy extension even has a specific function for the purpose. Just call this before you start outputting your html:
ob_start('ob_tidyhandler');