One of the reasons why POSIX regular expressions aren't as good as Perl-Compatible ones.
This is my first cut at comment-stripping. Note that it won't recognise <!----> as a comment (if there's nothing visible, it's "<>" with "!----" inside. If these are possible, they should be stripped out with a str_replace first, otherwise the regexp below will choke.
$buffer = preg_replace('/<!--(.(?!-->))*.-->/','',$buffer);
The space-compression is simplicity itself:
$buffer=preg_replace('/\s+/',' ',$buffer);
This will collapse tabs as well as spaces. To collapse newlins also, stick an 's' in after the last '/' in the regexp.