PHP Simple CSS string parser

gossysecy

New Member
I need to parse some CSS code like:\[code\]color: black;font-family:"Courier New";background:url('test.png');color: red;--crap;\[/code\]Into:\[code\]array ( 'color'=>'red', 'font-family'=>'"Courier New"', 'background'=>'url(\'test.png\')', '--crap'=>'')\[/code\]
  • I need to do this via PHP. I can see this done easily via regexp (well, easy to those that know it, unlike myself :-) ).
  • I need the resulting array to be "normalized", there should not be any trailing spaces between tokens, even if they were in the source.
  • Valueless css tokens should be included in the array as a key only. (see --crap)
  • Quotes (and values in general) should remain as is, except for extra formatting (spaces, tabs); easily removed via trim() or via the relevant regexp switch.
  • Please not that at this point, I specifically do not need a full CSS parser, ie, there is no need to parse blocks ( \[code\]{...}\[/code\] ) or selectors ( \[code\]a.myclass#myid\[/code\] ).
  • Oh, and considering I'll be putting this stuff in an array, it is perfectly ok if the last items ( \[code\]color:red;\[/code\] ) completely override the original items ( \[code\]color:black;\[/code\] ).
 
Back
Top