SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Put your best practices away. The New Guy's Avatar
    Join Date
    Sep 2002
    Location
    Canada
    Posts
    2,087
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)

    Parsing CSS Style Structure

    I need a script that can parse CSS stlye structure into a multidimentonal array.

    For example I need this:

    #Header{
    border: 1;
    font-family: "Arial";
    }

    Turn into an array like (Structure Wise):

    Array
    (
    [Header] => Array
    (
    [border] => 1
    [font-family] => Arial
    )
    )

    Thanks.
    "A nerd who gets contacts
    and a trendy hair cut is still a nerd"

    - Stephen Colbert on Apple Users

  2. #2
    SitePoint Zealot
    Join Date
    Jul 2005
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Play with the tokenizer tool of PHP, this should be possible to work with. Otherwise make your own tokenizer where you iterate through the string one by one. Joined with a nice state machine it's able to track this down. You would need to ignore all whitespaces, and recognise some things for a stylesheet (source: http://www.w3.org/TR/CSS1#appendix-b):

    identifier = [a-z][-]+[-a-z0-9][-]
    property = identifier
    semicolon = ;
    bracket_open = {
    bracket_close = }

    Then you can have something like (pseudo code):"

    while yypos is smaller then string length do:
    find identifier when STATE is NOT CSS_RULE_OPEN < yychar is starting with [a-z][-] and follows with [-a-z0-9][-] until white then SET state to IDENTIFIER
    find whitespace IGNORE
    if yychar is { then SET state to CSS_RULE_OPEN
    if yychar is } then SET state to CSS_RULE_CLOSE
    if yychar is : AND state is CSS_RULE_OPEN do SET state START_PROPERTY
    etc.

    I hope you get the point it's easy, I just throw away my printer stylesheet parser written in Pascal.

    PHP Code:
    while ( $ypos <= strlen$css ) ) {
         
    $yychar $css$ypos };
        
         
         if ( ( 
    $yychar == "{" && $yytokentype != CSS_RULE_OPEN ) ) {
                  
    $yytokentype CSS_RULE_OPEN;
                  
    $yytoken "";
         } else {
              die( 
    'Tokenizer Error: Unexpected { found in css rule' );
         }
         
    $yypos++;


  3. #3
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, with regular expressions it shouldn't be that scary

    PHP Code:
    $css "
    #Header, .footer {
        border: 1; font-family: Arial;
    }
    a:hover { color:red; }"
    ;

    $parsed = array();

    preg_match_all(
        
    "~([^{]+)\{(.*?)\}~s",
        
    $css,
        
    $m,
        
    PREG_SET_ORDER);

    foreach(
    $m as $p) {
        
    $decl = array();
        foreach(
    explode(";"$p[2]) as $e)
            if(
    count($e explode(":"$e)) > 1)
                
    $decl[trim($e[0])] = trim($e[1]);
        foreach(
    explode(","$p[1]) as $sel)
            
    $parsed[trim($sel)] = $decl;

    }
    print_r($parsed); 

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2005
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    regular expressions are scary For me , though


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •