简体   繁体   English

消除PHP中重复的代码

[英]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. 我在接下来的1000行中重复此代码,只是更改了$ pregao,$ uasg和$ url中[]中的数字。 Always increasing 1 number in each statement. 在每个语句中始终增加1个数字。 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. 它将在0到1000之间循环遍历该函数(您可以动态设置该值),每个循环向$ i加一个,然后向变量添加$ i。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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