regex - How to split a string by space, numbers, and quotes in PHP? -
i parsing string in php has 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 unquoted string , can have single or double quoted strings and/or numbers. can have 1 multiple key value pairs.
i need split string in 2 ways:
the first unquoted string not numeric.
the second extract numeric value and/or quoted strings - can single or dobule
thus need
- jobgrade
- p:parttime employee
- c:customer support
or
- somevariable
- 1:value1
- 2:value2
my thoughts:
i thought splitting string , iterating through test:
for 1: if value not numeric , not quoted variable name
for 2+: not sure easy way because must detect difference between keys , values:
question:
how can distinguish between key/value?
treat csv, , iterate on divide up. variable [0]
, keys odd starting [1]
, values [2]
.
var_dump(str_getcsv("jobgrade 'p' 'parttime employee' 'c' 'customer support'", ' ', "'"));
array(5) { [0]=> string(8) "jobgrade" [1]=> string(1) "p" [2]=> string(17) "parttime employee" [3]=> string(1) "c" [4]=> string(16) "customer support" }
Comments
Post a Comment