dtv8189 2014-01-09 17:38
浏览 65
已采纳

PHP Regex匹配特定的css类名+:before伪

Been more than 6 hours trying to find best PHP regex to match a specific class name + :before pseudo. For example I have css code like this

h1 {
  font-size: 3em;
  line-height: 1.65em;
}

p{line-height: 1.5em; margin: 10px 0;}

.action-admin:before{
   content: "";
   display: block;
   width: 50px;
   height: 50px;
}

.action-user:before{
   content: "";
   display: block;
   width: 20px;
   height: 20px;
}

footer{
   clear: both;
}

so, I want to search all css code that have class name match with '.action-{anyotherwords}:before' and listed them in a php variable (will process this to next action)

so, in simple words - I only want to get these code below from my css above

.action-admin:before{
   content: "";
   display: block;
   width: 50px;
   height: 50px;
}

.action-user:before{
   content: "";
   display: block;
   width: 20px;
   height: 20px;
}
  • 写回答

1条回答 默认 最新

  • dss524049 2014-01-09 17:45
    关注

    You can use this regex to find all these blocks:

    \.action-[\w-]+:before\s*?\{[^}]+\}
    

    enter image description here

    Example:

    preg_match_all("/\.action-[\w-]+:before\s*?\{[^}]+\}/", $css, $matches);
    $actionCSS = implode("
    
    ", $matches[0]);
    

    The CSS code with only the action blocks will then be in $actionCSS.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?