douwen9540
2016-10-05 18:31
浏览 170
已采纳

php获取css文件中的所有类名

Let's say I have a css file as shown...

span {
    //whatever
}

.block {
    //whatever
}

.block, .something {
    //whatever
}

.more,
h1,
h2 {
    //whatever
}

I want to extract all class names and put it into an array, but I want to keep the structure, so the array will look like...

["span", ".block", ".block, .something", ".more, h1, h2"]

So there are four items.

This is my attempt...

$homepage = file_get_contents("style.css");

//remove everything between brackets (this works)
$pattern_one = '/(?<=\{)(.*?)(?=\})/s';

//this regex does not work properly
$pattern_two = "/\.([\w]*)\s*{/";

$stripped = preg_replace($pattern_one, '', $homepage);
$selectors = array();
$matches = preg_match_all($pattern_two, $stripped, $selectors);

what is the proper regex to use for pattern 2?

图片转代码服务由CSDN问答提供 功能建议

假设我有一个如图所示的css文件...

 <  code> span {
 //whatever
}

.block {
 //whatever
}

.block,.something {
 // whatever what n} 
 
。 更多,
h1,
h2 {
 //无论什么
} 
   
 
 

我想提取所有类名并将其放入数组中,但我想要 保持结构,所以数组看起来像......

  [“span”,“。block”,“。block,.something”,“。more,  h1,h2“] 
   
 
 

所以有四个项目。

这是我的尝试......

  $ homepage = file_get_contents(“style.css”); 
 
 //删除括号之间的所有内容(这可行)
 $ pattern_one ='/(?&lt; = \ {  )(。*?)(?= \})/ s'; 
 
 //这个正则表达式无法正常工作
 $ pattern_two =“/\。([\\ w] *)\ s * {/”  ; 
 
 $ stripped = preg_replace($ pattern_one,'',$ homepage); 
 $ selectors = array(); 
 $ matches = preg_match_all($ pattern_two,$ stripped,$ selectors); 
   
 
 

用于模式2的​​正确正则表达式是什么?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题