<?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;
?>
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 response
Although correct and although it works on many versions of Php, the /e
option has been deprecated since the Php7 version so it is not a good way to go ...
I think that in the latest versions the recommended would be
$b=preg_replace_callback(
'/\|(.+?)\|(.+?)\|/',
function($m){return $m[0]. ($m[1]+$m[2]); },
$texto
);