-3

我需要获取 css 类名、属性和值,以便将内联 css 样式应用于 div。目前,我可以从这样的字符串中检索 css 类名信息:

$class = ".custom_class {
    margin-top: 0px !important;
    border-top-width: 0px !important;
    border-right-width: 0px !important;
    border-left-width: 0px !important;
    padding-top: 250px !important;
    padding-bottom: 250px !important;
    background-image: url(http://localhost/wordpress/wp-content/uploads/2015/01/old-car.jpg?id=3663) !important;
    border-left-color: #dd3333 !important;
    border-right-color: #dd3333 !important;
    border-top-color: #dd3333 !important;
}";

如何获取或正则表达式所有 css 属性和值,以便将它们包含在这样的 div 中:

<div class="custom_class" style="margin-top: 0px !important;border-top-width: 0px !important;border-right-width: 0px !important;border-left-width: 0px !important;padding-top: 250px !important;padding-bottom: 250px !important;background-image: url(http://localhost/wordpress/wp-content/uploads/2015/01/old-car.jpg?id=3663) !important;border-left-color: #dd3333 !important;border-right-color: #dd3333 !important;border-top-color: #dd3333 !important;"></div>

我试过这个:preg_match("/{(.*?)}/", $class, $match); 但它用括号输出。对于班级名称,我不知道该怎么做。

4

1 回答 1

1

您与您的正则表达式很接近preg_match("/{(.*?)}/", $class, $match);,只需转义大括号并检索以下数据$match[1]

preg_match("/^([\w.]+)\s*\{(.*?)\}/s", $class, $match);
$classname = $match[1];
$style = $match[2];
于 2015-01-24T12:54:37.017 回答