php switch case statement to handle ranges

You can specify the character range using regular expression. This saves from writing a really long switch case list. For example,

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

Well, you can have ranges in switch statement like:

//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;
}

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

*UPDATE: with default value *

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
    }
}