<?php
$s=str_replace("\r\n"," ",$text);
echo preg_replace("/((?<=>)|(?<=--)|(?<=.))[\s\n\r\t]+((?=--)|(?=<))/U",'',$s);?>
pagespeed on;
pagespeed EnableFilters collapse_whitespace;
>>> a = """
... <!DOCTYPE html>
... <html lang="en">
... <head>
... <meta charset="UTF-8">
... <title>Title</title>
... </head>
... <body>
...
... </body>
... </html>
... """
>>> import re
>>> a1 = a.replace("\r\n"," ")
>>> a1
'\n<!DOCTYPE html>\n<html lang="en">\n<head>\n <meta charset="UTF-8">\n <title>Title</title>\n</head>\n<body>\n\n</body>\n</html>\n'
>>> re.sub("/((?<=>)|(?<=--)|(?<=.))[\s\n\r\t]+((?=--)|(?=<))/U",'', a1)
'\n<!DOCTYPE html>\n<html lang="en">\n<head>\n <meta charset="UTF-8">\n <title>Title</title>\n</head>\n<body>\n\n</body>\n</html>\n'