多重组合逻辑应使用 switch 或 IF

Multiple combinations logic should use switch or IF

我正在尝试在开关中存在特定字符串时编写逻辑。

例如:字符串

* * * * * 我的字符串在每个字符串之间将始终采用这种格式 space 但字符串长度会有所不同。但是 5 spaces 和 5 string

其他例子: 0 * * * * * 0 * * * * * 12/3 * * * * 0 0 * 0 0 * * * * * * 0 0 * * * AA 12

我在这里尝试的是在字符串存在时编写 switch case,然后 * 我会 运行 case。

switch(true):
case '0 * * * * ':
echo 'First string has value';
case '`* * 0 0 * `':
echo '3rd & 4th string has value';
case 'A B C AA 12 ':
echo 'All string has value';

等...

我有很多组合,但我不确定 switch 是否是实现它的正确方法。

或逻辑 2:

我是否应该用 spaces 分解字符串然后使用 if 条件检查 5 个字符串?

类似这样的事情

   $total = 0;
   if( preg_match('/([^*\s]+)/', $str, $match ) ){
       $total = count( $matches[1]);
   }
    echo $total;

如果您需要知道它们在哪里匹配,请改用它

   $str = '* 0 * * *';
   if( preg_match_all('/([^*\s]+)|\s/', $str, $match ) ){
       print_r( $match )
   }

结果

array (
  0 => 
  array (
    0 => ' ',
    1 => '0',
    2 => ' ',
    3 => ' ',
    4 => ' ',
  ),
  1 => 
  array (
    0 => '',
    1 => '0',
    2 => '',
    3 => '',
    4 => '',
  ),
)

这样看(基于 0 的索引)您知道哪些匹配。匹配项位于数组索引 1 中,因此 $match[1][n]

然后你就可以像这样循环遍历

  $results = [];
   foreach( $match[1] as $m ){
      if( strlen($m) > 0 ){
          $results[] = "Match at pos $m";
      }
   }

等等...

优点是它们可能组合 * 和其他东西是巨大的,这规范化了,所以你只需要担心非 * 的事情。想象一下,如果为每一种可能的组合添加一个 if 的情况,那是不会发生的。

switch 语句与 if 语句本质上不同:

switch(true){
  case true:
    echo 'true';
  case false:
    echo 'false';
  case 'foo':
    echo 'foo';
}

因此,要回答您的问题,您可能无法使用 switch 语句。 您可以使用 中所示的正则表达式,但有时简单也是好的:

$list = [
  '0 * * * *',
  '* 0 * * *',
  '* * 12/3 * *',
  '* * 0 0 *',
  '0 0 * * *',
  '* * * 0 0',
  '* * * AA 12',
];

foreach($list as $item){
  if(count($cron = explode(' ', $item)) == 5){
    echo "Resultset of '$item'<br>";

    foreach($cron as $v){
      echo "$v<br>";
      // So I recommend if statements here.
    }
  } else {
    echo "'$item' is not in the correct format: '". implode(', ', $cron) . "'";
  }
}

您的问题 can/should 无需正则表达式即可解决。根据您希望编写纯英文输出的程度,您可以通过利用一些智能数组函数来避免使用一整套条件/开关案例。考虑这种方法:

代码:(Demo)

$strings=[
    '* * * * *',
    '0 * * * *',
    '* 0 * * *',
    '* * 12/3 * *',
    '* * 0 0 *',
    '0 0 * * *',
    '* * 0 0 0',
    '* * * AA 12',
    'A B C D E'
];
$positions=['1st','2nd','3rd','4th','5th'];

foreach($strings as $s){
    $result=array_intersect_key($positions,array_diff(explode(' ',$s),['*']));
    $count=sizeof($result);
    if(!$count){
        echo "No values found in string.\n";
    }elseif($count==5){
        echo "String is full of values.\n";
    }else{
        echo "Values found in: ",implode(', ',$result),"\n";
    }
}

输出:

No values found in string.
Values found in: 1st
Values found in: 2nd
Values found in: 3rd
Values found in: 3rd, 4th
Values found in: 1st, 2nd
Values found in: 3rd, 4th, 5th
Values found in: 4th, 5th
String is full of values.

我的 3 个嵌套数组函数是这样执行的:

  1. 在每个 space 上拆分字符串(假设您的值不包含 space...如果不是这样,您应该更改值分隔符)。
  2. 从新生成的数组中删除具有以下值的元素:* 并保留键。
  3. 仅引用具有非*值的元素,使用键查找位置名称以供显示。

与冗长的条件繁重的替代方法相比,此方法应大大压缩您的代码块。