Вот пример PHP, который очищает комментарии и помещает их все в одну строку.
<?php
$str = <<<EOT
/*
* Simple javascript function
* that writes a square of slashes
*/
function writeSlashes(rows, columns) {
for (row = 1; row < rows; row++ )
{
// The columns
for ( col = 1; col < columns; col++ ) {
s = (Math.floor((Math.random() * 2) % 2)) ? "╱" : "╲"; //Randomizing it
document.write(s);
}
document.writeln("<br>");
}
}
document.body.innerHTML = "";
document.writeln('<br/><a href="http://www.foo.bar?test"> foo.bar </a><br/><br/>');
writeSlashes(20,20);
EOT;
$re1 = '@
(\/\*[\s\S]*?\*\/\s*) # slash-star comments
| (\s+//.*$) # whitespaces before forward slashes till end of line
| (^\s+) # whitespaces at the start of a line
| (?<=[),;:?{}\[\]=<>*%+\-])(\s+) # whitespaces after specific characters
| (\s+)(?=[(,;:?{}\[\]=<>*%+\-]) # whitespaces before specific characters
| [\r\n]+(?!\w) # next line characters not followed by a word character
@mx';
$result = preg_replace($re1, '', $str);
# remaining multiple whitespaces or next line character to one space
$re2 = '/\s{2,}|[\r\n]+/';
$result = preg_replace($re2,' ', $result);
echo htmlentities($result);
Результат:
function writeSlashes(rows,columns){for(row=1;row<rows;row++){for( col=1;col<columns;col++){s=(Math.floor((Math.random()*2)%2))?"╱":"╲";document.write(s);}document.writeln("<br>");}}document.body.innerHTML="";document.writeln('<br/><a href="http://www.foo.bar?test">foo.bar</a><br/><br/>');writeSlashes(20,20);
Но этот метод не может сравниться с реальной минимизируемой библиотекой, которая могла бы заменить длинные имена переменных и функций на короткие имена.