Can you optimize this code?

7

Can you optimize this code to make it faster?

if (strpos($qt, "blood") !== FALSE){
  if (preg_match("/^blood (?<blood>.+)$/", $qt, $match)){
    switch ($match['blood']) {
      case "a+": 
        $result = "A+"; 
        $sndline = "Ideal donor: A+<br>Other donors: A+ or O+<br>Only if no Rh(+) found: A- or O-"; 
        break;
      case "a-": 
        $result = "A-"; 
        $sndline = "Ideal donor: A-<br>Other donors: A- or O-"; 
        break;
      case "b+": 
        $result = "B+"; 
        $sndline = "Ideal donor: B+<br>Other donors: B+ or O+<br>Only if no Rh(+) found: B- or O-"; 
        break;
      case "b-": 
        $result = "B-";
        $sndline = "Ideal donor: B-<br>Other donors: B- or O-"; 
        break;
      case "ab+": 
        $result = "AB+"; 
        $sndline = "Ideal donor: AB+<br>Other donors: AB+ or A+ or B+ or O+<br>Only if no Rh(+) found: AB- or A- or B- or O-"; 
        break;
      case "ab-": 
        $result = "AB-"; 
        $sndline = "Ideal donor: AB-<br>Other donors: AB- or A- or B- or O-"; 
        break;
      case "o-": 
        $result = "O-"; 
        $sndline = "Ideal donor: O-<br>Other donors: O-"; 
        break;
      case "o+": 
        $result = "O+"; 
        $sndline = "Ideal donor: O+<br>Other donors: O+<br>Only if no Rh(+) found: O-"; 
        break;
    }
  }
}
    
asked by anonymous 13.05.2015 / 02:06

1 answer

4

One way to optimize this code is to eliminate the use of regular expressions , as it involves the process of deploying the regex , which causes a overload , unless it is really necessary to use it, in fact, the PHP page function preg_match , mentions:

  

Do not use preg_match() if you just want to check if a string   appears in another string . Use strpos() or strstr() instead, they   will be faster.

One way to do this is to use the strpos function to find the position of a value in the string and with the substr function , extract it: / p>

function extrairPedaco($texto, $inicio, $delimitador){
    $sub = substr($texto, strpos($texto, $inicio) + strlen($inicio), strlen($texto));
    return substr($sub, 1, strpos($sub, $delimitador));
}

And to use it, do so:

$texto = "The blood A+ tend to be cooperative, sensitive, clever, passionate and smart.";
$referencia = "blood";
$tipos = ['+', '-'];

foreach ($tipos as $tipo){
    $pedaco = extrairPedaco($texto, $referencia, $tipo);
    $pedaco = strtoupper($pedaco); // Converte para maiúsculo, eliminando a necessidade de usar a variável "result".
        switch($pedaco){
            case "A+": 
                echo "Ideal donor: A+<br>Other donors: A+ or O+<br>Only if no Rh(+) found: A- or O-"; 
                break;
            case "A-":  
                echo "Ideal donor: A-<br>Other donors: A- or O-"; 
                break;
            case "B+":  
                echo "Ideal donor: B+<br>Other donors: B+ or O+<br>Only if no Rh(+) found: B- or O-"; 
                break;
            case "B-":  
                echo "Ideal donor: B-<br>Other donors: B- or O-"; 
                break;
            case "AB+": 
                echo "Ideal donor: AB+<br>Other donors: AB+ or A+ or B+ or O+<br>Only if no Rh(+) found: AB- or A- or B- or O-"; break;
            case "AB-": 
                echo "Ideal donor: AB-<br>Other donors: AB- or A- or B- or O-"; 
                break;
            case "O-":  
                echo "Ideal donor: O-<br>Other donors: O-"; 
                break;
            case "O+":  
                echo "Ideal donor: O+<br>Other donors: O+<br>Only if no Rh(+) found: O-"; 
                break;
            default:
                // Faça algo aqui caso as comparações acima falhem.
                echo "No blood found\n";
                break;
        } 
}

Exemplo

    
13.05.2015 / 21:49