i want to get logic from string that what from input string. For example: Input:
pageType == "static" OR pageType == "item" AND pageRef == "index"
How to i get logic like:
0 => pageType == "static"
1 => pageType == "item"
2 => pageRef == "index"
The logic clause must be complete based on what is entered.
I place like this:
$input = 'pageType == "static" OR pageType == "item" AND pageRef == "index"';
preg_match_all('/(. )(?!AND|OR)(. )/s', $input, $loX);
var_dump($loX);
but the array just show:
0 => pageType == "static" OR pageType == "item" AND pageRef == "index"
1 => pageType == "static" OR pageType == "item" AND pageRef == "index
2 => "
Please help me, thanks ^_^
CodePudding user response:
One option is to make use of the \G
anchor and a capture group:
\G(\w \h*==\h*"[^"]*")(?:\h (?:OR|AND)\h |$)
The pattern matches:
\G
Get continuous matches asserting the position at the end of the previous match from the start of the string(\w \h*==\h*"[^"]*")
Match 1 word characters==
and the value between double quotes(?:
Non capture group for the alternatives\h (?:OR|AND)\h
Match either OR or AND between spaces|
Or$
Assert the end of the string
)
Close the group
$re = '/\G(\w \h*==\h*"[^"]*")(?:\h (?:OR|AND)\h |$)/';
$str = 'pageType == "static" OR pageType == "item" AND pageRef == "index"';
preg_match_all($re, $str, $matches);
print_r($matches[1]);
Output
Array
(
[0] => pageType == "static"
[1] => pageType == "item"
[2] => pageRef == "index"
)
Another option to get the results is to split on the AND or OR surrounded by spaces:
$result = preg_split('/\h (?:OR|AND)\h /', $str);