1
I need to make a sum of values of an xml file. How little I’ve worked with Curl extracting data from another file or page, I don’t even know how to start. the file layout would be this.
0|0|0|0|3|0|0|46.000|
The value to be summed would be the 46,000 + the values of the next lines. More should be considered only the lines that had the value 3.
Does anyone have any code like that? I’ve searched and found nothing related.
Thanks for helping me, I’ll test it later.
– Willian Coqueiro
I tried so
<?php
$valores = file_get_contents('tce.txt');
preg_match_all('/\b\d+\|\d+\|\d+\|\d+\|3\|\d+\|\d+\|([\d\.]+)\b/', $valores, $encontrados);
$total = 0;
foreach ($encontrados as $encontrado) {
 print $total += $encontrado[7];
}
?>
– Willian Coqueiro
But I couldn’t get.
– Willian Coqueiro
Supplement the question with an excerpt from the file containing the data, to help resolve the problem.
– Rodrigo Rigotti
12356|743|4584|1|3|03|2016|127.000|
12356|747|9176|1|3|03|2016|1123.511|
12356|748|7559|1|3|03|2016|776.799|
12356|749|1467|1|3|03|2016|601.017|
Every beginning 12356| and a line and so on.– Willian Coqueiro
changed the regular expression and PHP code, try now.
– Rodrigo Rigotti
I used it as follows
$valores = file_get_contents('tce.txt');
preg_match_all('/\d+\|\d+\|\d+\|\d+\|3\|\d+\|\d+\|(\d+\.\d+)\|/', $valores, $encontrados);
$total = 0;
foreach ($encontrados[1] as $encontrado) {
 printf ($total += $encontrado);
}
And look at the value he returned to me from the following lines12356|743|4584|1|3|03|2016|127.000|
12356|747|9176|1|3|03|2016|1123.511|
12356|748|7559|1|3|03|2016|776.799|
12356|749|1467|1|3|03|2016|601.017|
Upshot=1271250.5112027.312628.327
– Willian Coqueiro
I’ve tried with what I found on the net. Nothing else is related. I noticed that then it is adding yes. In the result it gave
1271250.5112027.312628.327
the total sum was at the end after . 31, being 2628.327.– Willian Coqueiro
Sorry, I rewrote the code and ended up taking the call from
floatval
there in the middle. Try now.– Rodrigo Rigotti
Look how I used:
$valores = file_get_contents('tce.txt');
preg_match_all('/\d+\|\d+\|\d+\|\d+\|3\|\d+\|\d+\|(\d+\.\d+)\|/', 
$valores, $encontrados);
$total = 0;
foreach ($encontrados[1] as $encontrado) {
 print $total += floatval($encontrado);
}
and yet it still doesn’t work. And now it brings me that result:1271250.5112027.312628.327
will be that I am writing right there at the time of pulling the txt file?– Willian Coqueiro
Let’s go continue this discussion in chat.
– Willian Coqueiro
By the chat the friend helped me and the end of the code was like this
$valores = file_get_contents('tce.txt'); 
preg_match_all('/\d+\|\d+\|\d+\|\d+\|3\|\d+\|\d+\|(\d+\.\d+)\|/', $valores, $encontrados);
$total = 0;
foreach ($encontrados[1] as $encontrado) {
 $total += floatval($encontrado);
}
print $total;
– Willian Coqueiro