2

我想在我的 css 的每一行的开头添加一个类

我想

.more {
    padding: 5px 10px 0;
}

#fbook {
    display: inline;
    float: left;
    margin: 0 0 0 4px;
    width: 320px;
}

看起来像

.test .more {
    padding: 5px 10px 0;
}

.test #fbook {
    display: inline;
    float: left;
    margin: 0 0 0 4px;
    width: 320px;
}

像这样的东西。

^([A-Za-z0-9]+)$

如果我的 css 看起来像这样就可以了

more
{
    padding: 5px 10px 0;
}

fbook
{
    display: inline;
    float: left;
    margin: 0 0 0 4px;
    width: 320px;
}

但我似乎无法在找到 . # {

4

2 回答 2

2

尝试这个

$result = preg_replace('/(?i)^([.#a-z]+\{)$/m', '.test $1', $subject);

解释

"
(?im)         # Match the remainder of the regex with the options: case insensitive (i); ^ and \$ match at line breaks (m)
^             # Assert position at the beginning of a line (at beginning of the string or after a line break character)
(             # Match the regular expression below and capture its match into backreference number 1
   [.#a-z]       # Match a single character present in the list below
                    # One of the characters “.#”
                    # A character in the range between “a” and “z”
      +             # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
   \{            # Match the character “{” literally
)
\$             # Assert position at the end of a line (at the end of the string or before a line break character)
"
于 2012-07-23T05:11:41.017 回答
1

您可以使用像LESS这样的现代工具。只需获取您的代码,并将其包装为.test{ }

.test {
    .more {
        padding: 5px 10px 0;
    }

    #fbook {
        display: inline;
        float: left;
        margin: 0 0 0 4px;
        width: 320px;
    }
}

如果你只需要一次,你可以在网上做:http
: //leafo.net/lessphp/#demo客户端 JavaScript 选项。

于 2012-07-23T05:27:34.160 回答