Tidy also supports output buffering
The function ob_tidyhandler() is available for use with PHP output buffering to automatically parse, clean and repair output based on the tidy.default_config directive.

Using tidy output buffering
<?php
    ob_start
("ob_tidyhandler");
    
/* Data output here...*/
?>
Alternatively, you can set the tidy.clean_output php.ini directive to true to automatically clean output.

BE WARNED: Tidy does not check the content type of the output!