Sum of xml file values

1

I need to do a sum of values from an xml file. As I have not worked with Curl extracting data from another file or page, I do not even know how to start. the file layout would be this.

0|0|0|0|3|0|0|46.000|

The value to be added would be the 46,000 + values of the next lines. More should be considered only the lines that have the value 3.

Does anyone have any type code? I already researched and found nothing related.

    
asked by anonymous 21.06.2016 / 13:38

1 answer

2

Just use preg_match_all to find all rows that fit a certain pattern (including the 3 character in the fifth position), and then add all the values found in the 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);
}

If you do not know: the first argument of preg_match_all is a regular expression, and its purpose is to find all occurrences in a text that fit that expression - in this case, the format of the data you need.

    
21.06.2016 / 13:58