dongyong5255 2011-08-18 02:56
浏览 145
已采纳

CSS选择器的正则表达式 - PHP

Does anyone know what the most robust regex would be to select all selectors from a css declaration? Below is CSS code for non-standard CSS that would be a good test case.

.somenormalstyledeclaration 
{
    width:100%;
    height:100%;
    background-image: url(images/fallback-gradient.png);
}
.somenormalstyle1, .somenormalstyle2 
{
    width:100%;
    height:100%;
    background-image: url(images/fallback-gradient.png);
}
.gradient-bg 
{
   background-color: #1a82f7;
   background-image: url(images/fallback-gradient.png);
   background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#2F2727), to(#1a82f7));
   background-image: -webkit-linear-gradient(top, #2F2727, #1a82f7);
   background-image:    -moz-linear-gradient(top, #2F2727, #1a82f7);
   background-image:     -ms-linear-gradient(top, #2F2727, #1a82f7);
   background-image:      -o-linear-gradient(top, #2F2727, #1a82f7);
}

@-webkit-keyframes bounce_circle 
{
    0% 
    {
        opacity:0.3;
    }
    50% 
    {
        opacity:1;
        background-color:#111
    }
    100% 
    {
        opacity:0.3;
    }
}
  • 写回答

2条回答 默认 最新

  • douhuang2673 2011-08-18 04:01
    关注

    This should work mostly. It's a few steps, but not as good as a full parser.

    1. Replace all newlines with a space character
    2. Assuming you never use strings including { or }, you can replace /\{[^\}]\}/ with a ,
    3. Split all the text into an array on each , and ; character
    4. Trim all whitespace from the beginning and end of each selector

    Every element of the array should be a selector or an "at-rule" (i.e. @charset utf-8;).

    I have probably missed some other edge cases, but this should work as a quick-and-dirty solution for most cases.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊