[^{]+)(\{)(?P[^}]+)\}~', $line, $matches); foreach ($matches as $key => &$match) { if (is_numeric($key)) { continue; } $match = trim($match); if ($key === 'rules') { $ruleSet = array_filter(array_map('trim', explode(';', $match))); $match = []; foreach ($ruleSet as $rule) { $rule = array_map('trim', explode(':', $rule)); $match[$rule[0]] = $rule[1]; } } } $newCssArr[$matches['selector']] = $matches['rules']; } return $newCssArr; } /** * Convert incoming CSS array to proper CSS * * @param $array * @param $compressed boolean add break lines or not * @return string */ public static function arrayToCss($array, $compressed = true) { $css = ''; $break = ''; $space = ''; if (!$compressed) { $break = "\n\t"; $space = " "; } // rebuild CSS foreach ($array as $key1 => $value1) { $css .= $key1 . $space . '{'; foreach ($value1 as $key2 => $value2) { // in the case that the code is embedded in a media query if (is_array($value2)) { foreach ($value2 as $value3) { $css .= $break . $key2 . $space . '{'; foreach ($value3 as $mProp) { $css .= $break . $mProp . ':' . $value3 . ';'; } $css .= ($compressed) ? '}' : "\n}\n"; } } // regular selectors else { $css .= $break . $key2 . ':' . $value2 . ';'; } } $css .= ($compressed) ? '}' : "\n}\n"; } return $css; } }