How can line breaks be removed from PHP output to work with JavaScript document.write()?

When outputting PHP content that will be used with JavaScript's document.write() function, it's important to remove any line breaks from the PHP output to avoid syntax errors in the JavaScript code. One way to achieve this is by using the PHP function ob_start() to buffer the output, then ob_get_clean() to retrieve the buffered output without any line breaks.

<?php
ob_start();
// PHP code that generates output with line breaks
$output = ob_get_clean();
$output = str_replace(array("\r", "\n"), '', $output);
echo $output;
?>