<?php
$css=<<<CSS
p.special {
font: 12pt/14pt sans-serif;
margin: 5px 0px 2px 25px;
border: medium dashed #ff0000;
background: white url( http://www.foo.com/image.gif ) repeat-x fixed top right; }
/*************************************************
Save with a name ending in .css such as styles.css *************************************************/
h1 {
color: red; padding: 10px; text-decoration: underline;
}
.code_sample ul {
list-style-type: upper-roman; /* LEAVE THIS BIT OUT */
margin-left: 50px;
}
.code_sample p {
color: darkblue
}
a:link, a:visited, a:hover, a:active {
background-color: green;
color: white;
padding: 10px 25px;
text-align: center;
text-decoration: none;
display: inline-block;
}
CSS;
$patterns=[
'~\s+~', // reduce one or more consecutive whitespace characters ([\n\r \t]+) to a single space ...this combines the 2nd, 3rd, and 4th pattern from catchamonkey's answer
'~/\s*\*.*?\*/\s*|(?:(?<=[,:;{}(]) | (?=[,:;{})]))|;(?=})|^ | $~s' // comment blocks, leading/trailing spaces after ,:;{} characters, space before ), space after (, semicolon followed by }, whitespace at start/end of file
];
$replacements=[
' ',
''
];
// And here's a rabbit hole: https://regex101.com/r/nZbHND/1
var_export(preg_replace($patterns,$replacements,$css));