I need to parse some CSS code like:
color: black;
font-family:"Courier New";
background:url('test.png');
color: red;
--crap;
Into:
array (
'color'=>'red',
'font-family'=>'"Courier New"',
'background'=>'url(\'test.png\')',
'--crap'=>''
)
- 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 (
{...}
) or selectors (a.myclass#myid
). - Oh, and considering I'll be putting this stuff in an array, it is perfectly ok if the last items (
color:red;
) completely override the original items (color:black;
).