Reputation: 15365
I have a list of questions in a table, some of which are only to be displayed if certain criteria are met. A record might have criteria as simple as 4002=Y where 4002 is the question number and Y is the answer. If 4002=Y then the question is to be displayed.
For records with only one criteria I have no problem.
But then there are records that have criteria like the following:
402=Y OR 7003=Y OR 905=Y OR 7007=Y OR 7008=Y OR 7010=Y OR 7011=Y OR 7013=Y
In this case I would need to evaluate each option to see if the question is to be displayed or not.
Other questions will have similar strings; some shorter, some longer.
How would I best split the string up so I can evaluate each section at a time and still be able to compare them correctly?
I can reformat the data to some degree, but I would prefer not to if at all possible.
Is this a regex()
task (I'm not very familiar with that yet)? I've tried list()
, split()
and explode()
with little success.
Any pointers would be appreciated.
Upvotes: 1
Views: 278
Reputation: 460
This assumes the format you've specified and i'm assuming the possible values will either be Y (yes) or N (no)
$string = '402=Y OR 7003=Y OR 905=Y OR 7007=Y OR 7008=Y OR 7010=Y OR 7011=Y OR 7013=Y';
$regex = "/\b([\d]+)=([YN])\b/";
if (preg_match_all($regex, $string, $matches)) {
print_r($matches);
}
Should give you:
Array
(
[0] => Array
(
[0] => 402=Y
[1] => 7003=Y
[2] => 905=Y
[3] => 7007=Y
[4] => 7008=Y
[5] => 7010=Y
[6] => 7011=Y
[7] => 7013=Y
)
[1] => Array
(
[0] => 402
[1] => 7003
[2] => 905
[3] => 7007
[4] => 7008
[5] => 7010
[6] => 7011
[7] => 7013
)
[2] => Array
(
[0] => Y
[1] => Y
[2] => Y
[3] => Y
[4] => Y
[5] => Y
[6] => Y
[7] => Y
)
)
Upvotes: 0
Reputation: 1153
I'd first run a regex against the string to change the "separator" into "&" -- especially in a situation where there may be extra whitespace around the "OR"'s (easier to express that in regex, than it is to try to adjust/trim each term individually. Then use parse_str()
and you have an array in which each index is the question number and the value is "Y" or "N".
Upvotes: 0
Reputation: 522626
$criteria = '402=Y OR 7003=Y OR 905=Y OR 7007=Y OR 7008=Y OR 7010=Y OR 7011=Y OR 7013=Y';
$rules = array();
foreach (explode(' OR ', $criteria) as $criterium) {
$rule = explode('=', $criterium);
$rules[$rule[0]] = ($rule[1] == 'Y');
}
var_dump($rules);
// array() {
// [402]=> bool(true)
// [7003]=> bool(true)
// [905]=> bool(true)
// ...
// }
$isAnyRuleTrue = in_array(true, $rules);
Upvotes: 1
Reputation: 343135
you can try
if (strpos($record,"OR" )!==FALSE){
$s = explode("OR",$record);
foreach ($s as $k){
#do something with $k
}
}
Upvotes: 0
Reputation: 239341
If your input string really is just a bunch of simple criteria separated with " OR ", then a simple explode() will indeed do the trick:
$criteria = "402=Y OR 7003=Y OR 905=Y OR 7007=Y OR 7008=Y OR 7010=Y OR 7011=Y OR 7013=Y";
$split_criteria = explode("OR", $criteria);
foreach ($split_criteria as $single)
{
echo trim($single) . "\n";
}
However if it is more complicated (if you allow AND as well as OR, say) then you will need a correspondingly smarter parser.
Upvotes: 3