.class{padding:10px 5px;color:#fff;padding:50px!important;background:#000;padding:10px;}
function clear_string($test_string) {
$test_rules_match = preg_match('/(?P<selector>[^\{\s*]+)\{(?P<rules>[^\}]+)\s*\}/ui', $test_string, $test_string_parts);
$test_selector = $test_string_parts['selector'];
$test_rules = $test_string_parts['rules'];
$new_test_string_array = []; $clean_string = $test_string;
if(preg_match_all('/(?P<prop>[a-z]+):(.*?)(?P<important>!important)*;/i',$test_rules,$matches,PREG_PATTERN_ORDER)) {
foreach ($matches['prop'] as $id=>$selector) {
if(!isset($new_test_string_array[$selector]) || !preg_match('~important~ui',$new_test_string_array[$selector]) || $matches['important'][$id]>'')
$new_test_string_array[$selector] = $matches[0][$id];
}
$clean_string = $test_selector.' {'.implode('',$new_test_string_array).'}';
}
return($clean_string);
}
<?php
$str= ".class{padding:10px 5px;color:#fff;padding:50px!important;background:#000;padding:10px;}";
preg_match("/^([^\{]+){/", $str, $strOut);
$strOut= $strOut[0];
echo "<h5>Исходная строка:</h5> $str <hr>";
preg_match_all("~([^\{\;]+?)\:([^\;\}]+)~i", $str, $out);
$arr= [];
foreach($out[1] as $i => $p) {
if(!in_array($p, $arr)) {
// echo 'Unik - ' . $p . '<br>';
$strOut.= $p . ':' . $out[2][$i] . '; ';
$arr[]= $p;
}
}
echo '<hr>$arr=<br>';
print_r ($arr);
echo '<h5>Результат:</h5>' . $strOut;
?>