HTML Tidy has this infuriating habit of putting a newline before the closing tag. For example:
Some text
In php you can use:
$buffer = preg_replace('/\n<\//smUi','',$buffer);