简体   繁体   中英

Eliminate repeated code in PHP

I have this piece of code below:

<?php
//Dados do Pregão para pegar dados e para o email
$pregao = ($newuasg[2]);
$uasg = ($newuasg[3]);
$url = ("http://www.comprasnet.gov.br/livre/Pregao/mensagens_acomp.asp?prgcod=$nova[1]");
$contents = file_get_contents($url, false, $context);

//Verifica a data/hora da mensagem com a data/hora atual
$a=preg_match_all("/\<span class\=\"mensagem2\"\>(.*?)\<\/span\>/",$contents,$b);
$date = ($b[1][0]);
$date = str_replace("(", "", $date);
$date = str_replace(")", "", $date);
$newdate = $date[6] . $date[7] . $date[8] . $date[9] . $date[2] . $date[3] . $date[4] . $date[5] . $date[0] . $date[1] . $date[10] . $date[11] . $date[12] . $date[13] . $date[14] . $date[15] . $date[16] . $date[17] . $date[18];

//Verifica se alguma das palavras chaves foi encontrada
$resultado = procpalavras($contents, $palavras);

//Envia email caso ache qualquer das palavras indicadas
if ($resultado === null) {
echo "Pregão:" . $pregao . " - Uasg" . $uasg . " - Palavras não encontradas";
} else if ($newdate <= $envio) {
} else {
include 'mail.php';
}
?>

I repeat this code for the next 1000 lines just changing the number in [] for $pregao, $uasg and $url. Always increasing 1 number in each statement. Is there a way to make a code that i dont need to repeat this code over and over again?

Use:

for ($i=0; $i < 1000; $i++) {
    $pregao = ($newuasg[1 + $i]);
    $uasg = ($newuasg[2 + $i]);      
}

It will loop through the function from 0 to 1000 ( you can set that value dynamically) adding one to $i every loop, then adding $i to your variables.

It may help you

// here is the loop which will iterate over 1000 time
 // feel free to change the 1000 to what you prefer
 for ($x = 0; $x < 100; $x++)
 {
     // acquiring the params
     $pregao = ($newuasg[0+($x*2)]);
     $uasg = ($newuasg[1+($x*2)]);
     $url = ("http://www.comprasnet.gov.br/livre/Pregao/mensagens_acomp.asp?prgcod=" . $nova[0+$x]);

    $contents = file_get_contents($url, false, $context);

    //Verifica a data/hora da mensagem com a data/hora atual
    $a=preg_match_all("/\<span class\=\"mensagem2\"\>(.*?)\<\/span\>/",$contents,$b);
    $date = ($b[1][0]);
    $date = str_replace("(", "", $date);
    $date = str_replace(")", "", $date);
    $newdate = $date[6] . $date[7] . $date[8] . $date[9] . $date[2] . $date[3] . $date[4] . $date[5] . $date[0] . $date[1] . $date[10] . $date[11] . $date[12] . $date[13] . $date[14] . $date[15] . $date[16] . $date[17] . $date[18];

    //Verifica se alguma das palavras chaves foi encontrada
    $resultado = procpalavras($contents, $palavras);

    //Envia email caso ache qualquer das palavras indicadas
      if ($resultado === null) {
    echo "Pregão:" . $pregao . " - Uasg" . $uasg . " - Palavras não encontradas";
} else if ($newdate <= $envio) {
} else {
       include 'mail.php';
      }
 }
?>
 <?php

 // here is the loop which will iterate over 1000 time
 // feel free to change the 1000 to what you prefer
 for ($x = 0; $x < 100; $x++)
 {
     // acquiring the params
     $pregao = ($newuasg[0+($x*2)]);
     $uasg = ($newuasg[1+($x*2)]);
     $url = ("http://www.comprasnet.gov.br/livre/Pregao/mensagens_acomp.asp?prgcod=" . $nova[0+$x]);

    $contents = file_get_contents($url, false, $context);

    //Verifica a data/hora da mensagem com a data/hora atual
    $a=preg_match_all("/\<span class\=\"mensagem2\"\>(.*?)\<\/span\>/",$contents,$b);
    $date = ($b[1][0]);
    $date = str_replace("(", "", $date);
    $date = str_replace(")", "", $date);
    $newdate = $date[6] . $date[7] . $date[8] . $date[9] . $date[2] . $date[3] . $date[4] . $date[5] . $date[0] . $date[1] . $date[10] . $date[11] . $date[12] . $date[13] . $date[14] . $date[15] . $date[16] . $date[17] . $date[18];

    //Verifica se alguma das palavras chaves foi encontrada
    $resultado = procpalavras($contents, $palavras);

    //Envia email caso ache qualquer das palavras indicadas
      if ($resultado === null) {
    echo "Pregão:" . $pregao . " - Uasg" . $uasg . " - Palavras não encontradas";
    } else if ($newdate <= $envio) {
    } else {
       include 'mail.php';
      }
 }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM