I am parsing a string in PHP that has the following pattern
VARIABLE Key1 Value1 Key2 Value2 Key3 Value3 ...
looks like:
JOBGRADE 'P' 'Parttime Employee' 'C' 'Customer Support'
OR
SOMEVARIABLE 1 "Value1" 2 'Value2'
This line begins with a line without quotes and may contain single or double quotes and / or numbers. It can have one pair pair with several keys.
I need to break the string in two ways:
The first is to get a string without quotes, which is not numeric.
The second, to extract a numerical value and / or quoted strings - can be single or dobule
So I need
- JOBGRADE
- P: Part-time employee
- C: Customer Service
OR
- SOMEVARIABLE
- 1: Value 1
- 2: Value2
My thoughts:
I thought about splitting a string and repeating its test:
for 1: if the value is not numeric and is not quoted, this is the name of the variable
for 2+: Not sure if this is an easy way to do this, because I have to determine the difference between keys and values:
Question
How can I distinguish key / value?
source share