When using the function tidy_repair_string with the option show-body-only turned on to process pieces of HTML, I had to use trim() to remove the carriage returns that this function at the end of the resulting string.
function trimmed_tidy_repair_string($html){
$config["show-body-only"]=true;
$html=tidy_repair_string($html,$config,"utf8");
$html=trim($html);
return $html;
}