php - 样式数组到常规 CSS

标签 php css

我有一个包含所有样式的数组,我必须通过在 PHP 中循环将它们更改为正确的 CSS。

例如,

Array
(
    [#outlook a] => Array
        (
            [padding] => Array
                (
                    [0] => 0
                )

        )

    [body] => Array
        (
            [width] => Array
                (
                    [0] => 100% !important
                )

            [-webkit-text-size-adjust] => Array
                (
                    [0] => none
                )

            [margin] => Array
                (
                    [0] => 0
                )

            [padding] => Array
                (
                    [0] => 0
                )

            [background-color] => Array
                (
                    [0] => #FAFAFA
                )

        )
[.preheaderContent div a] => Array
    (
        [color] => Array
            (
                [0] => #336699
            )

        [font-weight] => Array
            (
                [0] => normal
            )

        [text-decoration] => Array
            (
                [0] => underline
            )

    )

[.preheaderContent div a:visited] => Array
    (
        [color] => Array
            (
                [0] => #336699
            )

        [font-weight] => Array
            (
                [0] => normal
            )

        [text-decoration] => Array
            (
                [0] => underline
            )

    )

)

像这样,我将有一个包含所有样式信息的大数组。所以我必须将其转换为正确的 CSS。我知道这很简单。但是我在递归循环的某个地方失败了。这是我为此使用的功能。

function cssbuild(&$cssArray)
{
    foreach ($cssArray as $selector => $style) 
    {
        if(is_array($style))
        {
            return $this->cssbuild($style);
        }
        else
        {
            $cssArray[$selector] = $cssArray[$selector].":".$style.";";
        }
    }
}

任何关于如何做到这一点的想法将不胜感激...

最佳答案

试试这个:

function createCSS($cssArray)
{
    $css = '';
    foreach ($cssArray as $classname => $properties)
    {
        $css .= "$classname{";
        foreach( (array) $properties as $propertyname => $propertyvalue )
        $css .= $propertyname .": ".$propertyvalue[0].";";
        $css .= "}";
    }
    return $css;
}

关于php - 样式数组到常规 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15221760/

相关文章:

php - PDO 连接从命令行工作,但不是通过 Apache?

php - 将分数转换为单词

php - IIS7 服务器上的 Web 服务用于 Apache 服务器上的网站

jquery - 如何在单选按钮上禁用 jquery 日期选择器

html - Heroku:在应用程序启动时显示自定义页面

javascript - 使用新参数通过服务器端处理重绘 Bootstrap 数据表

具有多个 RDBMS 的 PHP、Python、Ruby 应用程序

css - 如果调整页面大小,左侧菜单有拥抱间隙

html - Svg 蒙版/剪切路径问题

html - 在纯 css 图片上向下滑动