Read . txt, set delimiter, remove duplicates

Asked

Viewed 89 times

0

Good afternoon. I need my script to read a . txt like the example below:

4984538078766798|11|2016|246 
// Na primeira linha $a teria o valor de "4984538078766798"
// $b teria o valor de 11
// $c teria o valor de 2016
// $d teria o valor de 246
4108637744329741|07|2017|241
// Na segunda linha $a teria o valor de "4108637744329741"
// $b teria o valor de 07
// $c teria o valor de 2017
// $d teria o valor de 241
4984012022438078|08|2016|757 
// Na terceira linha $a teria o valor de "4984012022438078"
// $b teria o valor de 08
// $c teria o valor de 2016
// $d teria o valor de 757 

Ignore empty lines and remove duplicates.

I’ve tried it this way and failed:

    $list = dirname(__FILE__) . "/lista.txt";

$content = file_get_contents($list);
$txt = preg_split("/[\r\n]/", $content, -1, PREG_SPLIT_NO_EMPTY);
$separa = trim('|', $txt);
$a = trim($separa[0]);
$b = trim($separa[1]);
$c = trim($separa[2]);
$d = trim($separa[3]);

1 answer

0


One way to avoid element duplicity is to use the element as the index of an array. So every time you play the same index on it, it will be disregarded. So, from what I understand of your question you want two things:

  1. Disregarding duplicate lines
  2. Have a 'field' for each value found, properly 'exploded' with the delimiter '|'

The example below does this (you can also test on https://ideone.com/DfPnqn):

<?php
$linhas = array('123|45|1234|101', '999|88|777|666', '123|45|1234|101');
foreach ($linhas as $linha)
    $arr[$linha] = NULL; // aqui se removem as linhas duplicadas
echo "*** LINHAS ***\n";
foreach ($arr as $key => $valor) { 
    echo $key . "\n"; // aqui se comprova o resultado final sem duplicatas
    $partes = explode ('|', $key);
    foreach ($partes as $parte) {
        $var[$parte] = NULL; // aqui se cria um array que resume todos os números sem duplicatas
    }
}
echo "*** CAMPOS ***\n";        
foreach ($var as $key => $valor)
    echo $key . "\n"; // aqui se comprova o resultado de todos os campos finais sem duplicatas

Since I don’t have your file, I simulated 3 lines within an array:

$linhas = array('123|45|1234|101', '999|88|777|666', '123|45|1234|101');

In your case, simply disregard the code up to line 4, but insert the line read in $arr[$linha] = NULL.

When executing, it results in:

*** LINHAS ***
123|45|1234|101
999|88|777|666
*** CAMPOS ***
123
45
1234
101
999
88
777
666

As you can see, all the two arrays I used receive NULL because they have no need to store anything in them, were used only to remove any duplicities.

https://ideone.com/DfPnqn

  • Thank you so much for trying to help, buddy :)

  • @Gennie, there you are using the ';' tab and your statement you used '|'.

  • It is not clear how your txt file, as it is every line, will be easier if you edit your original question and put there a two or 3 original lines.

  • Yes, I’ll do it right now.

  • Ready friend :)

  • 1

    Ok, now I get it. In your program, you are reading ALL the file into a string using file_get_contents. If you want to process EVERY LINE individually, you can read with fgets as in this example: https://www.w3schools.com/php/showphp.asp?filename=demo_file_feof. Hence, for each line you can do the explode and take the 4 pieces, placing them in the desired variables.

  • Got it, buddy, thanks. How can I count how many lines there are in my txt ?

  • I need to create a variable to count the lines, for as long as there are remaining lines in txt it runs the rest of the code and when there are no remaining lines it stops.

  • I did it this way: $myfile = fopen($ccs_list, "r+") or die("Unable to open file!");&#xA;$ccs = fgets($myfile);&#xA;$partes = explode('|', $ccs);&#xA;$cc = trim($partes[0]);&#xA;$b = trim($partes[1]);&#xA;$c = trim($partes[2]);&#xA;$d = trim($partes[3]);

Show 5 more comments

Browser other questions tagged

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