У меня проблема с тем, что я не могу правильно преобразовать мой массив обратно в строку. Я продолжаю заканчивать вопросами форматирования из Wazoo.
Я нашел то, что, как я думал, сработает, но он экспортирует только первый элемент в массиве, в отличие от всех элементов в массиве, а также включает дополнительные атрибуты.
Вот что я запускаю:
<?php
/**
* Converts a multidimensional array of CSS rules into a CSS string.
*
* @param array $rules
* An array of CSS rules in the form of:
* array('selector'=>array('property' => 'value')). Also supports selector
* nesting, e.g.,
* array('selector' => array('selector'=>array('property' => 'value'))).
*
* @return string
* A CSS string of rules. This is not wrapped in <style> tags.
*/
function grasmash_generate_css_properties($rules, $indent = 0) {
$css = '';
$prefix = str_repeat(' ', $indent);
foreach ($rules as $key => $value) {
if (is_array($value)) {
$selector = $key;
$properties = $value;
$css .= $prefix . "$selector {\n";
$css .= $prefix .grasmash_generate_css_properties($properties, $indent + 1);
$css .= $prefix . "}\n";
}
else {
$property = $key;
$css .= $prefix . "$property: $value;\n";
}
}
return $css;
}
$css = "
.text_1_1 {
width: 419px;
height: 101.5px;
background-size: 1459.5px 632.5px;
background-position: -486px -255.5px;
}
.text_1_2 {
width: 393.5px;
height: 49.5px;
background-size: 1459.5px 632.5px;
background-position: -7px -398.5px;
}
";
preg_match_all( '/(?ims)([a-z0-9\s\,\.\:#_\-@]+)\{([^\}]*)\}/', $css, $arr);
$result = array();
foreach ($arr[0] as $i => $x)
{
$selector = trim($arr[1][$i]);
$rules = explode(';', trim($arr[2][$i]));
$result[$selector] = array();
foreach ($rules as $strRule)
{
if (!empty($strRule))
{
$rule = explode(":", $strRule);
$pxless = str_replace("px","", trim($rule[1]));
$arr = explode(" ", $pxless);
for($j=0; $j<count($arr); $j++) $arr[$j]=($arr[$j]/2)."px";
$finally = implode(" ", $arr);
$result[$selector][][trim($rule[0])] = $finally;
}
}
}
$finalCss = grasmash_generate_css_properties($result);
print $finalCss
?>
, что приближает меня к решению, но печать такова:
.text_1_1 {
0 {
width: 209.5px;
}
1 {
height: 50.75px;
}
2 {
background-size: 729.75px 316.25px;
}
3 {
background-position: -243px -127.75px;
}
}
1 {
}
Я чувствую, что foreach l oop просто отсутствует Уровень этого, и поэтому я получаю только первый элемент, а также неправильное форматирование.