fre*_*oma 5 php regex preg-replace
我正在尝试使用preg_replace缩小多个CSS文件.实际上,我只是想从文件中删除任何换行符/标签和注释.以下为我工作:
$regex = array('{\t|\r|\n}', '{(/\*(.*?)\*/)}');
echo preg_replace($regex, '', file_get_contents($file));
但我想在单个多行正则表达式中执行此操作,如下所示:
$regex = <<<EOF
{(
\t
|
\r
|
\n
|
/\*(.*?)\*/
)}x
EOF;
echo preg_replace($regex, '', file_get_contents($file));
但是,这根本不起作用.有没有办法做到这一点?
编辑:好的,所以我将看看现有的minifiers,但它仍然让我有一个问题,我将如何做这样的多行正则表达式,因为使用x修饰符多行正则表达式应该可以正常工作,即使在PHP,应该'他们?
我不确定你会怎么做,但这是我朋友写的一个脚本,它在缩短CSS方面非常快:
function minimize_css($input)
{
// Remove comments
$output = preg_replace('#/\*.*?\*/#s', '', $input);
// Remove whitespace
$output = preg_replace('/\s*([{}|:;,])\s+/', '$1', $output);
// Remove trailing whitespace at the start
$output = preg_replace('/\s\s+(.*)/', '$1', $output);
// Remove unnecesairy ;'s
$output = str_replace(';}', '}', $output);
return $output;
}
Run Code Online (Sandbox Code Playgroud)