I am parsing a string in PHP that has the following pattern
VARIABLE Key1 Value1 Key2 Value2 Key3 Value3 ...
similar to:
JOBGRADE 'P' 'Parttime Employee' 'C' 'Customer Support'
OR
SOMEVARIABLE 1 "Value1" 2 'Value2'
This line starts with an unquoted string and can have single or double quoted strings and/or numbers. It can have one to multiple key value pairs.
I need to split the string in 2 ways:
The first to get the unquoted string that is not numeric.
The second to extract the numeric value and/or quoted strings – can be single or dobule
Thus I need
- JOBGRADE
- P:Parttime Employee
- C:Customer Support
OR
- SOMEVARIABLE
- 1:Value1
- 2:Value2
My Thoughts:
I thought about splitting the string and iterating through it to test:
for 1: If value is not numeric and not quoted it is the variable name
for 2+: Not sure the easy way to do this because I must detect the difference between the keys and values:
Question:
How can I distinguish between the key/value?
Treat it as CSV, and iterate over it to divide it up. The variable is
[0], keys are odd starting from[1], values even from[2].