views:

142

answers:

5

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.

+3  A: 

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.

caf
Thanks, we do have some ANDs in there but I am looking over things to make sure that they are not intermingled. If all the questions contain only ORs or only ANDs I'll be OK
Jason
A: 

you can try

if (strpos($record,"OR" )!==FALSE){
    $s = explode("OR",$record);
    foreach ($s as $k){
      #do something with $k
    }
}
ghostdog74
+1  A: 
$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);
deceze
A: 

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
        )

)

rezzif
I tried the above but I get nothing from print_r($matches); Am I just missing something? (or am I dense?)
Jason
just edited to add the result you should get from print_r. So you're not getting that? Even when you directly copy and paste that code?
rezzif
A: 

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".

grantwparks