• You are not logged in. | Login

Post a reply

  • Index
  •  » Help me!
  •  » how I can process the css and get styles’ description?

January 16, 2007 9:55 am

sirburpsalot
Member
Ranks

how I can process the css and get styles’ description?

hi everyone

could you tell me please how I can process the css and get styles’ description?

i.e. example, input:

CSS
.testClassName {
  param: value;
  param1: value;
}

Output: class name and associative array

className = ".testClassName";
params["param"] = "value";
params["param1"] = "value";

thank u.


 

 

January 16, 2007 9:58 am

sam43inwood
Member
Ranks

Re: how I can process the css and get styles’ description?

here i have  a simple example, if you want you can add it:

<?php 
function parser_css( &$in_string ) 
{ 
    $_CSS = array(); 
     
    preg_match_all( '#((?:\.|[\w-0-9]+\.)[\w-0-9]+)\s+\{(.*?)\}#is', $in_string, $matches ); 
     
    if ( isset( $matches[1] ) ) { 
        $size = sizeof( $matches[1] ); 
         
        for ( $i = 0; $i < $size; $i++ ) { 
             
            $matches[1][ $i ] = trim( $matches[1][ $i ] ) ; 
            $pos = strpos($matches[1][ $i ], '.' ); 
             
            if ( $pos === 0 ) { 
                 
                $matches[1][ $i ] = substr( $matches[1][ $i ], 1 ); 
            } else { 
             
                $tmp = explode( '.', $matches[1][ $i ] ); 
                 
                $element          = $tmp[0];                 
                $matches[1][ $i ] = $tmp[1]; 
            } 
             
            $_CSS[ $matches[1][ $i ] ] = array(); 
            if ( isset( $element ) ) { 
                $_CSS[ $matches[1][ $i ] ]['element'] = $element; 
            } 
             
            if ( isset( $matches[2][ $i ] ) ) { 
                $matches[2][ $i ] = trim( $matches[2][ $i ] ); 
                preg_match_all( '#([0-9\w-]+)\s?\:\s?([0-9\w-]+)\;#is', $matches[2][ $i ], $submatches ); 
                 
                if ( isset( $submatches[1] ) ) { 
                     
                    $subsize = sizeof( $submatches[1] ); 
                     
                    for ( $j = 0; $j < $subsize; $j++ ) { 
                         
                        $_CSS[ $matches[1][ $i ] ]['params'][ trim( $submatches[1][ $j ] ) ] = trim( $submatches[2][ $j ] ); 
                    } 
                } 
            } 
             
        } 
    } 
     
    return $_CSS; 
} 
$string = ' 
.testClassName { 
  param : value; 
  param1: value; 
} 
.testClassName2 { 
  param: value; 
  param1: value; 
} 
div.testClassName3 { 
  param: value; 
  param1: value; 
}'; 
$css = parser_css( $string ); 
echo '<pre>'; 
print_r($css); 
>

Last edited by sam43inwood (January 16, 2007 10:01 am)


 

 
  • Index
  •  » Help me!
  •  » how I can process the css and get styles’ description?
  • Actions
  • Top
ITCrimea. Ukraine Web Development Company. Professional Developers and Web Designers Team
Custom Web Designs, Internet Applications, E-Commerce Websites, Interactive Sites, Database-Driven Sites and Services