Sum of xml file values

Asked

Viewed 1,015 times

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.

1 answer

2


Just use preg_match_all to find all the lines that fit into a particular pattern (including the character 3 in fifth position), and then sum all values found in eighth position of each line:

preg_match_all('/\d+\|\d+\|\d+\|\d+\|3\|\d+\|\d+\|(\d+\.\d+)\|/', 
$valores, $encontrados);
$total = 0;
foreach ($encontrados[1] as $encontrado) {
    $total += floatval($encontrado);
}

In case you don’t know: the first argument of preg_match_all is a regular expression, and its purpose is to find all occurrences in a text that fits into that expression - in this case, the data format you need.

  • Thanks for helping me, I’ll test it later.

  • I tried so <?php&#xA;$valores = file_get_contents('tce.txt');&#xA;preg_match_all('/\b\d+\|\d+\|\d+\|\d+\|3\|\d+\|\d+\|([\d\.]+)\b/', $valores, $encontrados);&#xA;$total = 0;&#xA;foreach ($encontrados as $encontrado) {&#xA; print $total += $encontrado[7];&#xA;}&#xA;?>

  • But I couldn’t get.

  • Supplement the question with an excerpt from the file containing the data, to help resolve the problem.

  • 12356|743|4584|1|3|03|2016|127.000|&#xA;12356|747|9176|1|3|03|2016|1123.511|&#xA;12356|748|7559|1|3|03|2016|776.799|&#xA;12356|749|1467|1|3|03|2016|601.017| Every beginning 12356| and a line and so on.

  • changed the regular expression and PHP code, try now.

  • I used it as follows $valores = file_get_contents('tce.txt');&#xA;preg_match_all('/\d+\|\d+\|\d+\|\d+\|3\|\d+\|\d+\|(\d+\.\d+)\|/', $valores, $encontrados);&#xA;$total = 0;&#xA;foreach ($encontrados[1] as $encontrado) {&#xA; printf ($total += $encontrado);&#xA;} And look at the value he returned to me from the following lines 12356|743|4584|1|3|03|2016|127.000|&#xA;12356|747|9176|1|3|03|2016|1123.511|&#xA;12356|748|7559|1|3|03|2016|776.799|&#xA;12356|749|1467|1|3|03|2016|601.017| Upshot= 1271250.5112027.312628.327

  • 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.

  • Sorry, I rewrote the code and ended up taking the call from floatval there in the middle. Try now.

  • Look how I used: $valores = file_get_contents('tce.txt');&#xA;preg_match_all('/\d+\|\d+\|\d+\|\d+\|3\|\d+\|\d+\|(\d+\.\d+)\|/', &#xA;$valores, $encontrados);&#xA;$total = 0;&#xA;foreach ($encontrados[1] as $encontrado) {&#xA; print $total += floatval($encontrado);&#xA;} 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?

  • By the chat the friend helped me and the end of the code was like this $valores = file_get_contents('tce.txt'); &#xA;preg_match_all('/\d+\|\d+\|\d+\|\d+\|3\|\d+\|\d+\|(\d+\.\d+)\|/', $valores, $encontrados);&#xA;$total = 0;&#xA;foreach ($encontrados[1] as $encontrado) {&#xA; $total += floatval($encontrado);&#xA;}&#xA;print $total;

Show 7 more comments

Browser other questions tagged

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