fork download
  1. <?php
  2.  
  3. $string = "This is the total 98+84+67 of the numbers 98, 84 and 67 if we add.";
  4. preg_match("/[\d]+\+[\d+]+/",$string,$matches);
  5. $numbers = explode("+",$matches[0]);
  6. $sum = array_sum($numbers);
  7. print preg_replace("/[\d]+\+[\d+]+/",$sum,$string);
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
This is the total 249 of the numbers 98, 84 and 67 if we add.