Pergunta

I want to add tab to all the properties here but I don't know how to isolate the properties $1 is not working here.

Small tweak would make this working...

Thanks!

$css = <<<EOF

body {
padding : 0px;
margin: 0px;
line-height: 10px;
}

p {
z-index: 9;
font-size: 10px;
}

EOF;


echo preg_replace('#({)(.*?)(})#', '\t$1' , $css);

/*

I'm expecting to get all properties with TAB :

    body {
         padding : 0px;
         margin: 0px;
         line-height: 10px;
    }

    p {
           z-index: 9;
           font-size: 10px;
    }
*/
Foi útil?

Solução

You can try this:

$pattern = '~(?:{|\G(?!\A))\s*?;?\s*\K[^;}]+~';
echo preg_replace($pattern, "\t$0" , $css);

pattern details:

(?:          # possible beginings of the match
    {        # a curly bracket
  |          # OR
    \G(?!\A) # the end of a precedent match
)
\s*?;?\s*    # spaces (after the curly bracket) or ending ; of a property 
\K           # resets all the begining from match result
[^;}]+       # all that is not a closing curly bracket or a semi-colon
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top