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

Asked

Viewed 450 times

0

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


<?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 txt data

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

I need you to check example the dado2 with the dado2 next line and tell if it’s duplicated?

  • If you tried any code?

  • For example here you can read the file https://imasters.com.br/artigo/1134/php/php-lendo-um-arquivo-txt?trace=1519021197&source=single

  • My code is already listing txt thus data list($dado1,$dado2,$dado3,$dado4,$dado5,$dado6) and inside txt thus: dado1 | dado2 | dado3 | dado4 | dado5 | dado6 |

  • missing file example!?

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

  • I paste an example with 3 lines (in question) and one repeating and is to do what when repeat? (See the question has to have more detailed) I can even help you from that.

  • I edited the question I put the example! Sorry I’m new to this forum!

  • @Rafa is just the date2 or just check in the second column ?

  • Yes friend only the data2

  • I managed to do to check if there is the data2 but I wanted to see if this duplicate understands?

  • vc can use the in_array($given, $array) and check if the data is in the array, but the thing I see already has technical error when the guy comments "opens the DATABASE" with the file fopen... vc know that if your site is on, even by google da to find your "database" right?

  • I am using in host place to register customers of a pizzeria, with this only I have access! But how I use this function you mentioned?

  • I understand just create a new array and scan the list summing.

  • How do I do it buddy can make the code for me?

  • ???????????????

Show 11 more comments

2 answers

2

Function in_array(variable, array) aims to check whether the object is contained in the array, as the name says it checks whether the value is in the array. 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 he will enter the first if. More questions the official documentation: https://www.w3schools.com/php/func_array_in_array.asp

PS: Dùvidas like this one are easily solved in other questions already asked here in the group. Before posting read the documentation

To research how a whole does it:

if(strpos(file_get_contents("texto.txt"),$string)) {
    echo "tem";
}else{
    echo "não tem";
}
  • vc will place it in an array by 'punching' each line in a line array or if it is only an element of the Voce line you can make the explode by space (" "), up there I taught you to compare the array with the variable, now figure out how to put the isolated value in the array values

  • put option 2 with affection!

  • I was going to let you know the in_array before discovering the easy way, but to extreme good mood hj!

  • at the end of the code I put a code that searches the entire TXT file behind a variable... if there is a variable it returns a true, if it does not return a false, another alternative would be you count +1 each time it appears and check the value at the end. The methods and formats are these... you can apply them as is best for you.

  • Poxa! will give a studied, old...

  • help is not doing for Voce. I gave you 2 ways, I explained how to do, I took the step by step, chewed, now it’s with Voce!

  • @Rafa as Michel mentioned this site is not to do the work for others. It is to help learn and solve problems. It’s important to understand that to better integrate into the community.

Show 2 more comments

0

You can use this code to read the file .txt line by line checking 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;
        }
    }
}
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.