2016-11-13 79 views
0

我想将样式表分割成数组,每个选择器都以他的样式分隔数组。到目前为止,我有这个代码,它运行良好,但不存在媒体查询的情况。我想实现的是将媒体查询中的选择器拆分为单个数组,但每个数组都有他的媒体查询选择器。 这是实施例的风格HTMLPHP - 将样式表分割成数组

<style> 
#abc{ background-color: gray; font-size: 10px;} 
.abc { background-color: gray; font-size: 10px;} 

@media only screen and (max-width:640px) { 
    #abc { 
    height: 200px !important; 
    } 
} 

@media only screen and (max-width:640px) { 
    #abcd { 
    height: 200px !important; 
    } 
    .abcd { 
    height: 200px !important; 
    } 
} 
</style> 

这是代码,我到目前为止有:

$dom = new DomDocument(); 
@$dom->loadHTML($html); 
$para = $dom->getElementsByTagName('style'); #DOMNodeList 
if ($para instanceof DOMNodeList) { 
    foreach ($para as $node) { 
     printf ($node->nodeValue); 
} 
} 
$file = $node->nodeValue; 

$arrs = explode('}', $file); 

for ($i = 0; $i < count($arrs); $i++) 
{ 
    echo 'style ' . $i . ' - ' . $arrs[$i] . '<br />'; 
} 

这是uotput我越来越:

style 0 - #abc{ background-color: gray; font-size: 10px; 
style 1 - .abc { background-color: gray; font-size: 10px; 
style 2 - @media only screen and (max-width:640px) { #abc { height: 200px !important; 
style 3 - 
style 4 - @media only screen and (max-width:640px) { #abcd { height: 200px !important; 
style 5 - .abcd { height: 200px !important; 

通缉输出:

style 0 - #abc{ background-color: gray; font-size: 10px; 
style 1 - .abc { background-color: gray; font-size: 10px; 
style 2 - @media only screen and (max-width:640px) { #abc { height: 200px !important; 
style 3 - 
style 4 - @media only screen and (max-width:640px) { #abcd { height: 200px !important; 
style 5 - @media only screen and (max-width:640px) { .abcd { height: 200px !important; 

什么是w应该是正确的方法来获得?

+0

'如果($段的instanceof的DOMNodeList){':这个测试是没用的,'$ para'始终是一个'DOMNodeList'情况下,即使没有任何'风格'标签。为什么使用'printf($ node-> nodeValue);'?写'echo $ node-> nodeValue;',不需要格式化。 –

+0

要回答你的主要问题,正确的方法是propably找到一个CSS解析库:http://github.com/sabberworm/PHP-CSS-Parser –

+0

@CasimiretHippolyte,我试过,但分析器不支持媒体查询。这是一个开放的问题仍然 – codexy

回答

0

你可以尝试这样的事情,只要记住这个答案,需要知道或确保媒体查询的样式表的最后一部分,有一个媒体和彼此之间声明没有其他款式,下面你的例子的格式。然后,你可以做这样的事情:

... 
$file = $node->nodeValue; 
$parts = explode('@media', $file); 


// The first part of the stylesheet, where there are no media queries 
$styleCount = 0; 
$arrs = explode('}', $parts[0]); 
for ($i = 0; $i < count($arrs); $i++) 
{ 
    echo 'style ' . $i . ' - ' . $arrs[$i] . '<br />'; 
    $styleCount++; 
} 

// The media queries, remember to validate that there exists at least one before 
for ($i = 1; $i < count($parts); $i++){ 
    $arrs = explode('{', $parts[$i], 2); 
    $title = "@media " . $arrs[0]; 

    $arrIn = explode('}', $arrs[1]); 
    for($j = 0; $j < count($arrIn); $j++){ 
     // Validate for an empty space or a new line, it could be just something like strlen($arrIn[$j]) > 3 
     if(strlen($arrIn[$j]) > 4){ 
      echo 'style ' . $styleCount . ' - ' . $title . " { " . $arrIn[$j] . '</br>'; 
      $styleCount++; 
     } 
    } 
} 
+0

这是我得到的输出: 风格1 - 。 abc {background-color:gray; font-size:10px; (最大宽度:640px)样式4 - 仅媒体屏幕和(最大宽度:640px)样式5 - 仅媒体屏幕和(最大宽度:640px)样式2 - 样式3 - 宽度:640像素){ 式6 - @媒体仅屏幕和(最大宽度:640像素){ 风格7 - @媒体仅屏幕和(最大宽度:640像素){ 风格8 - @媒体仅屏幕和(最大-width:640像素){ 风格9 - @媒体只是屏幕(最大宽度:640像素){ – codexy

+0

对不起我尝试编辑答案,我做了一些修正,比如使用的'$ arrIn [$ J]'$,而不是我 – Moyom

+0

输出中唯一的变化是空的样式是#2而不是#3: 'style 0 - #abc {background-color:gray; font-size:10px; style 1 - .abc {background-color:gray; font-size:10px; 风格2 - 风格3 - @media只有屏幕和(最大宽度:640px){#abc {height:200px!important; 风格4 - @media只有屏幕和(最大宽度:640px){#abcd {height:200px!important; 风格5 - @media唯一的屏幕和(最大宽度:640px){.abcd {height:200px!important; ' – Moyom