2012-01-16 11 views
30

Sto analizzando del testo e calcolando il peso in base ad alcune regole. Tutti i personaggi hanno lo stesso peso. Ciò renderebbe la dichiarazione dell'interruttore molto a lungo possibile utilizzare gli intervalli nell'istruzione case.dichiarazione caso switch php per gestire gli intervalli

Ho visto una delle risposte che difendevano gli array associativi.

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

Qual è il modo migliore per ottenere qualcosa di simile? C'è qualcosa di simile all'istruzione case bash in php? Sicuramente la scrittura di ogni singolo carattere nell'array associativo o nell'istruzione switch non può essere la soluzione più elegante o è l'unica alternativa?

risposta

1
$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: con il valore di default *

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

Ottimo approccio. – nikhil

+0

In questo modo, come aggiungere un caso predefinito. Di 'qualcosa che non si trova e voglio assegnargli un peso fisso. – nikhil

+0

Trovato qualcosa che non corrisponde a nessuno dei casi. In questo momento non viene assegnato alcun peso ma voglio un valore predefinito diverso da zero. – nikhil

123

Beh, si può avere intervalli di istruzione switch come:

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

Grazie, questo è veramente utile. – nikhil

+1

omg typus typecast, così ovvio! grazie –

+0

Molto bene !. Ma nessuno può accettarlo. 40 againts 1 voto. Scommetti che guadagni qualche letto. – vladkras

1

È possibile specificare l'intervallo di caratteri utilizzando espressioni regolari. Questo evita di scrivere una lista di casi di switch davvero lunga. Ad esempio,

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 
1

credo lo farei in modo semplice.

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

doSomething() verrà sempre chiamato, perché "switch ($ t = 100)" assegna da 100 a $ t –

Problemi correlati