将字符串分成几部分

发布于 2024-07-30 03:33:24 字数 561 浏览 4 评论 0原文

我在表格中有一个问题列表,其中一些问题只有在满足某些条件时才会显示。 记录的标准可能像 4002=Y 一样简单,其中 4002 是问题编号,Y 是答案。 如果4002=Y则显示问题。

对于只有一个标准的记录我没有问题。

但有些记录具有如下标准:

402=Y OR 7003=Y OR 905=Y OR 7007=Y OR 7008=Y OR 7010=Y OR 7011=Y OR 7013=Y

在这种情况下,我需要评估每个选项以查看是否要显示问题。

其他问题也会有类似的字符串; 有的较短,有的较长。

我如何最好地分割字符串,以便我可以一次评估每个部分,并且仍然能够正确地比较它们?

我可以在某种程度上重新格式化数据,但如果可能的话我宁愿不这样做。

这是一个 regex() 任务吗(我对此还不太熟悉)? 我尝试过 list()split()explode() 但收效甚微。

任何指示将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

守不住的情 2024-08-06 03:33:24

如果您的输入字符串确实只是一堆用“ OR ”分隔的简单条件,那么一个简单的explode()确实可以解决问题:

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

但是如果它更复杂(如果您允许AND以及OR,比如说)那么您将需要一个相应更智能的解析器。

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.

别挽留 2024-08-06 03:33:24
$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);
$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);
夏末染殇 2024-08-06 03:33:24

你可以试试

if (strpos($record,"OR" )!==FALSE){
    $s = explode("OR",$record);
    foreach ($s as $k){
      #do something with $k
    }
}

you can try

if (strpos($record,"OR" )!==FALSE){
    $s = explode("OR",$record);
    foreach ($s as $k){
      #do something with $k
    }
}
十雾 2024-08-06 03:33:24

我首先对字符串运行正则表达式,将“分隔符”更改为“&” -- 特别是在“OR”周围可能有额外空格的情况下(在正则表达式中更容易表达,而不是尝试单独调整/修剪每个术语。然后使用 parse_str() 并且您有一个数组,其中每个索引都是问题编号,值为“Y”或“N”。

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

终遇你 2024-08-06 03:33:24

这假设您指定的格式,并且我假设可能的值是 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
        )

)

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
        )

)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文