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

  1. jobgrade
  2. p:parttime employee
  3. c:customer support

or

  1. somevariable
  2. 1:value1
  3. 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

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -