消除PHP中重复的代码

时间:2018-01-31 17:27:20

标签: php

我在下面有这段代码:

<?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';
}
?>

我为接下来的1000行重复这段代码只需更改$ pregao,$ uasg和$ url的[]中的数字。始终在每个语句中增加1个数字。 有没有办法制作一个我不需要一遍又一遍地重复这段代码的代码?

3 个答案:

答案 0 :(得分:0)

使用:

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

它将遍历函数从0到1000(您可以动态设置该值)在每个循环中向$ i添加一个,然后将$ i添加到变量中。

答案 1 :(得分:0)

它可能对你有帮助

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

答案 2 :(得分:0)

 <?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';
      }
 }
?>
相关问题