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]);
Thank you so much for trying to help, buddy :)
– user113606
@Gennie, there you are using the ';' tab and your statement you used '|'.
– Rogério Dec
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.
– Rogério Dec
Yes, I’ll do it right now.
– user113606
Ready friend :)
– user113606
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 withfgets
as in this example: https://www.w3schools.com/php/showphp.asp?filename=demo_file_feof. Hence, for each line you can do theexplode
and take the 4 pieces, placing them in the desired variables.– Rogério Dec
Got it, buddy, thanks. How can I count how many lines there are in my txt ?
– user113606
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.
– user113606
I did it this way:
$myfile = fopen($ccs_list, "r+") or die("Unable to open file!");
$ccs = fgets($myfile);
$partes = explode('|', $ccs);
$cc = trim($partes[0]);
$b = trim($partes[1]);
$c = trim($partes[2]);
$d = trim($partes[3]);
– user113606
Let’s go continue this discussion in chat.
– Rogério Dec