0

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.

4

5 に答える 5

3

入力文字列が「 OR 」で区切られた単純な条件の集まりである場合、単純な expand() が実際にそのトリックを行います:

$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";
}

ただし、より複雑な場合 (たとえば、OR だけでなく AND も許可する場合)、それに応じてよりスマートなパーサーが必要になります。

于 2009-07-27T02:48:07.950 に答える
1
$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);
于 2009-07-27T03:00:26.837 に答える
0

あなたが試すことができます

if (strpos($record,"OR" )!==FALSE){
    $s = explode("OR",$record);
    foreach ($s as $k){
      #do something with $k
    }
}
于 2009-07-27T02:50:09.460 に答える
0

これは、指定した形式を想定しており、可能な値は Y (はい) または N (いいえ) のいずれかであると想定しています


$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
        )

)

于 2009-07-27T03:00:34.437 に答える
0

最初に文字列に対して正規表現を実行して、「区切り文字」を「&」に変更します。特に、「OR」の周りに余分な空白がある可能性がある状況では(正規表現で表現する方が簡単です)各用語を個別に調整/トリミングしようとする. 次にparse_str()、各インデックスが質問番号で、値が「Y」または「N」である配列を使用します.

于 2009-07-27T03:05:26.410 に答える