php switch case语句来处理范围

时间:2012-01-16 06:51:18

标签: php range switch-statement

我正在解析一些文本并根据一些规则计算权重。所有角色都具有相同的重量。这会使switch语句真的很长,我可以在case语句中使用范围。

我看到了一个提倡关联数组的答案。

$weights = array(
[a-z][A-Z] => 10,
[0-9] => 100,
['+','-','/','*'] => 250
);
//there are more rules which have been left out for the sake of clarity and brevity
$total_weight = 0;
foreach ($text as $character)
{
  $total_weight += $weight[$character];
}
echo $weight;

实现这样的目标的最佳方法是什么? 是否有类似于PHP中的bash case语句? 当然,在关联数组或switch语句中写下每个字符都不是最优雅的解决方案,还是唯一的替代方案?

6 个答案:

答案 0 :(得分:146)

好吧,你可以在switch语句中使用范围,如:

//just an example, though
$t = "2000";
switch (true) {
  case  ($t < "1000"):
    alert("t is less than 1000");
  break
  case  ($t < "1801"):
    alert("t is less than 1801");
  break
  default:
    alert("t is greater than 1800")
}

//OR
switch(true) {
   case in_array($t, range(0,20)): //the range from range of 0-20
      echo "1";
   break;
   case in_array($t, range(21,40)): //range of 21-40
      echo "2";
   break;
}

答案 1 :(得分:2)

$str = 'This is a test 123 + 3';

$patterns = array (
    '/[a-zA-Z]/' => 10,
    '/[0-9]/'   => 100,
    '/[\+\-\/\*]/' => 250
);

$weight_total = 0;
foreach ($patterns as $pattern => $weight)
{
    $weight_total += $weight * preg_match_all ($pattern, $str, $match);;
}

echo $weight_total;

* 更新:使用默认值*

foreach ($patterns as $pattern => $weight)
{
    $match_found = preg_match_all ($pattern, $str, $match);
    if ($match_found)
    {
        $weight_total += $weight * $match_found;
    }
    else
    {
        $weight_total += 5; // weight by default
    }
}

答案 2 :(得分:1)

您可以使用正则表达式指定字符范围。这样就省去了写一个很长的开关案例列表。例如,

function find_weight($ch, $arr) {
    foreach ($arr as $pat => $weight) {
        if (preg_match($pat, $ch)) {
            return $weight;
        }   
    }   
    return 0;
}

$weights = array(
'/[a-zA-Z]/' => 10, 
'/[0-9]/'    => 100,
'/[+\\-\\/*]/'   => 250 
);
//there are more rules which have been left out for the sake of clarity and brevity
$total_weight = 0;
$text = 'a1-';
foreach (str_split($text) as $character)
{
  $total_weight += find_weight($character, $weights);
}
echo $total_weight; //360

答案 3 :(得分:0)

许多不同的方法来实现。

 $var = 0;
 $range_const = range(10,20);
 switch ($var) {
    case 1: $do = 5; break; # 1
    case 2: $do = 10; break; # 2
    case 3:
    case 4:
    case 5: $do = 15; break; # 3, 4, 5
    default:
      if ($var > 5 && $var < 10) { # High performance (6..9)
        $do = 20;
      } else if (in_array($var, $range_const, true)) { # Looks clear (10..20)
        $do = 25;
      } else { # NOT in range 1..20
        $do = -1;
      }
}
print($do);

没有直接范围X..Y进行比较,因为$ var在每个步骤中都检查为true,但这可以做一些很好的作弊...

$in = create_function('$a,$l,$h', 'return $a>=$l && $a<=$h;');
$var = 4;
switch (true) {
  case ($var === 1): echo 1; break;
  case ($var === 2): echo 2; break;
  case $in($var, 3, 5): echo "3..5"; break;
  case $in($var, 6, 10): echo "6..10"; break;
  default: echo "else";
} 

答案 4 :(得分:0)

如果条件比较复杂,可以将它们包装在函数中。这是一个过于简化的示例:

$chartID = 20;
$somethingElse = true;

switch (switchRanges($chartID, $somethingElse)) {
  case "do this":
      echo "This is done";
      break;
  case "do that":
      echo "that is done";
      break;
  default:
      echo "do something different";
}

function switchRanges($chartID, $somethingElse = false)
{
    if (in_array($chartID, [20, 30]) && $somethingElse === true) {
        return "do this";
    }
    if (in_array($chartID, [20, 50]) && $somethingElse === false) {
        return "do that";
    }
}

答案 5 :(得分:-1)

我想我会以一种简单的方式做到这一点。

switch($t = 100){
    case ($t > 99 && $t < 101):
        doSomething();
        break;
}