Do I need to check if a line is duplicated in my TXT how do I?

0

I need to check if a line is duplicated in my TXT how do I?

<?php
$divisao = "1000000000000";
$linha  = file("clientes/clientes.txt"); //Abre o banco de dados
$total  = count($linha); //Conta as linhas
$paginas = ceil($total/$divisao)-1; //Faz a divisão

if(!$pagina='$pagina;' ){$pagina = "0";}
else if(!is_numeric($pagina)){$pagina = "0";}
else if($pagina > $paginas){$pagina = "0";}
else{$pagina = $pagina;}
if($total<=0){
    echo "<P><DIV style=\"font-family:arial; font-size:14px; color:#999; text-align:center; margin-left:0 auto;\">Sem clientes para exibir!</DIV>";
}


$inicio = $pagina*$divisao;
$final  = $inicio+$divisao;
$final  = ($final > $total) ? $total : $final;
$linha = array_reverse($linha);
for ($i = $inicio; $i < $final; $i++){
list($dado1,$dado2,$dado3,$dado4,$dado5,$dado6) = explode("|",$linha[$i]);
echo "<form method=\"POST\" action=\"form.php\" onSubmit=\"if(!confirm('Deseja realmente continuar o pedido?')){return false;}\">";
echo "<tr><td>";

echo "<div id=\"div-clientes\">";


echo "<span style=\"display:none;\">$dado2</span>";
echo "<input type=\"text\" name=\"id\" value=\"$dado1\" id=\"campo_cads_id\" readonly=\"true\">";
echo "<input type=\"text\" name=\"data\" value=\"$dado6\" id=\"campo_cads_data\" readonly=\"true\">";
echo "<input type=\"text\" name=\"telefone\" value=\"$dado2\" id=\"campo_cads_tel\">";
echo "<input type=\"text\" name=\"nome\" value=\"$dado3\" id=\"campo_cads\">";
echo "<input type=\"text\" name=\"endereco\" value=\"$dado4\" id=\"campo_cads_end\">";
echo "<input type=\"text\" name=\"referencia\" value=\"$dado5\" id=\"campo_cads\">";

?>
  

Example of data contained in txt

DADO1|DADO2|DADO3|DADO4|DADO5|DADO6|
DADO1|DADO2|DADO3|DADO4|DADO5|DADO6|
DADO1|DADO2|DADO3|DADO4|DADO5|DADO6|

I need to check the dado2 example with the dado2 of the next line and say if it is duplicate?

    
asked by anonymous 13.11.2017 / 21:11

2 answers

2

Function in_array (variable, array) is to check if the object is contained in the array, as the name says it checks if the value is in the array. It will return true or false, see the example below:

<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");

if (in_array("Glenn", $people))
  {
  echo "Encontrado";
  }
else
  {
  echo "Não encontrado";
  }
?>

In this case, Glenn is in the array, so it will enter the first if. More doubts the official documentation: link

PS: Doubts like this are easily solved in other questions already asked here in the group. Before posting please read the documentation

To search as a whole does this:

if(strpos(file_get_contents("texto.txt"),$string)) {
    echo "tem";
}else{
    echo "não tem";
}
    
13.11.2017 / 22:45
0

You can use this code to read the file .txt line by line checking the occurrences on each line:

<?php
$dado2 = "DADO2";

$arquivo = file("teste.txt");
$encontrar = $dado2;
$repetido = false;

if ($arquivo !== false)
{
    foreach($arquivo as $index => $line)
    {
         if (preg_match("/$encontrar/", trim($line), $matches))
        {
            if($repetido){
               echo 'A linha '. ($index+1) .' contém repetido '.$encontrar . '<br />';
            }else{
               echo 'A linha '. ($index+1) .' contém '.$encontrar . '<br />';
            }
            $repetido = true;
        }
    }
}
?>
    
14.11.2017 / 15:18