<?php
$texto = "
|5,00|7,00||
|10,00|2,00||
|3,00|30,00||"; // "texto" da pergunta
$texto= str_replace(",",".",$texto); // locales (ver @ValdeirPsr)
$b=preg_replace('/\|(.+?)\|(.+?)\|/e', '"$0" . ($1+$2) ', $texto );
echo $b;
?>
By replacing the second argument of preg_replace
will contain strings as
"|5,00|7,00|" . (5,00 + 7,00)
which after calculated give |5,00|7,00|12
Update: ignore this answer
Although correct and despite working on many versions of Php, the option /e
was discontinued from the Php7 version so it is not a good way to go...
I think that in the most recent versions the recommended would be
$b=preg_replace_callback(
'/\|(.+?)\|(.+?)\|/',
function($m){return $m[0]. ($m[1]+$m[2]); },
$texto
);
@Andersoncarloswoss, thank you. Opps, I seem to have an older version... (PHP 5.6.9-1 )
– JJoao
Parse error: syntax error, Unexpected '$m' (T_VARIABLE) in [...][...] on line 10 LINE 10 >>> Function($m){retrun $m[0]. ($m[1]+$m[2]); },
– user60252
@Leocaracciolo, thank you, no more wine for this glass: "Return" and not "retrun"...
– JJoao
hehehe, has more Notice: A non well Formed Numeric value encountered in [...][...] on line 10
– user60252
test here http://sandbox.onlinephpfunctions.com/
– user60252
@Leocaracciolo, thanks for the suggestion: help!. It worked well with me. (do not forget to replace "," by "." included in the initial reply)
– JJoao
show, now yes I can give +1 :) https://ideone.com/unD8bq
– user60252