Preg_match_all help for extracting all css class

Discussion in 'PHP' started by samirkumardas, Jul 20, 2008.

  1. #1
    I have following file

    .active{ color:#333333; }
    input,select,.titulo1{
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 11px;
    }
    
    textarea{
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 10px;
    border: 1px solid #000000;
    }
    
    td,.Estilo1 {
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 10px;
    }
    
    hr {
    line-height: 1px;
    color: #000000;
    }
    Code (markup):

    I need to extract only css class not other thing like ID etc. That means I need all those thing that are start with dot(.).

    Desired Output of above code:

    .active{ color:#333333; }
    input,select,.titulo1{
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 11px;
    }
    Code (markup):


    Since .active is the only css class. \


    Please help to give me a pattern

    Thanks
     
    samirkumardas, Jul 20, 2008 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    If .active is the only CSS class, then why is your desired output:
    ...?

    .active is just:
    
    .active{ color:#333333; }
    
    Code (markup):
    Anyway, this seems to work for me:
    
    '~(?:^|[\s\}>])((\.[\w-]+)\s*\{[^\}]+\})~'
    
    PHP:
    (Assuming you JUST want to get .active, in this case.)
     
    nico_swd, Jul 21, 2008 IP
  3. cornetofreak

    cornetofreak Peon

    Messages:
    170
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    
    
    <?php
    $x = ".active{ color:#333333; }
    input,select,.titulo1{
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 11px;
    }
    
    textarea{
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 10px;
    border: 1px solid #000000;
    }
    
    td,.Estilo1 {
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 10px;
    }
    
    hr {
    line-height: 1px;
    color: #000000;
    }";
    preg_match_all("/(\,|\..*?)\{/si",$x,$y);
    print_r($y);
    
    ?>
    
    
    
    
    PHP:
    try that

    RETURNS

    Array ( [0] => Array ( [0] => .active{ [1] => .titulo1{ [2] => .Estilo1 { ) [1] => Array ( [0] => .active [1] => .titulo1 [2] => .Estilo1 ) )
     
    cornetofreak, Jul 21, 2008 IP