I want to get values of command tags (GET, FROM, IN, etc.) My command is:
// My command
$_cmd = 'GET a, b FROM p IN a and c="I am from Sarajevo" or d>1 ';
// My parser
if(preg_match_all('/(GET|FROM|IN)\s+([^\s]+)/si',$_cmd, $m))
$cmd = array_combine($m[1], $m[2]);
Output:
Array
(
[GET] => a,
[FROM] => p
[IN] => a
[from] => Sarajevo"
)
I am looking for this output:
Array
(
[GET] => a, b
[FROM] => p
[IN] => a and c="I am from Sarajevo" or d>1
)
As you see, problem is with whitespaces and repeated command tags in strings (like from). So how can I parse this command?
You cannot easily parse that with a single regex. (It’s doable, but not simple.)
You should use a simple tokenizer, where a regex again becomes a useful tool:
This gives you a simple list, where you just have to find the clauses that you are interested in, then remerge the subsequent tokens (though I’m confused about your use case):