protected function removeComments($text)
{
$look = array('url(', '//', '/*', '"', "'");
$out = '';
$min = null;
while (true) {
// find the next item
foreach ($look as $token) {
$pos = strpos($text, $token);
if ($pos !== false) {
if (!isset($min) || $pos < $min[1]) {
$min = array($token, $pos);
}
}
}
if (is_null($min)) {
break;
}
$count = $min[1];
$skip = 0;
$newlines = 0;
switch ($min[0]) {
case 'url(':
if (preg_match('/url\\(.*?\\)/', $text, $m, 0, $count)) {
$count += strlen($m[0]) - strlen($min[0]);
}
break;
case '"':
case "'":
if (preg_match('/' . $min[0] . '.*?(?<!\\\\)' . $min[0] . '/', $text, $m, 0, $count)) {
$count += strlen($m[0]) - 1;
}
break;
case '//':
$skip = strpos($text, "\n", $count);
if ($skip === false) {
$skip = strlen($text) - $count;
} else {
$skip -= $count;
}
break;
case '/*':
if (preg_match('/\\/\\*.*?\\*\\//s', $text, $m, 0, $count)) {
$skip = strlen($m[0]);
$newlines = substr_count($m[0], "\n");
}
break;
}
if ($skip == 0) {
$count += strlen($min[0]);
}
$out .= substr($text, 0, $count) . str_repeat("\n", $newlines);
$text = substr($text, $count + $skip);
$min = null;
}
return $out . $text;
}