-1

I have a text area, it include string and number.

enter image description here

I want to sum 3 record into a text like this

enter image description here

I try to show you my stack, hope you can help me. Thank you!

SantiBailors
  • 1,596
  • 3
  • 21
  • 44
XCode2015
  • 193
  • 1
  • 2
  • 10

2 Answers2

1

With a little regular expression

$s = "xxx = 230.5
bbb = 490.3
ccc = 3.948";

preg_match_all('/[,\.\d]+/', $s, $match);
print_r($match);
exit;

Result

Array
(
[0] => Array
    (
        [0] => 230.5
        [1] => 490.3
        [2] => 3.948
    )

)

Please regard: if you use comma and dot, you'll have to prepare the values for a valid floating point format.

Honk der Hase
  • 2,459
  • 1
  • 14
  • 26
1

I dont know, how to do that this would work with comma, but this code

If(isset($_POST['test'])) { //test is my textarea name
    $total = 0;
    $ex = explode(' ',$_POST['test']);
    function total ($ex) {
        global $total;
        return $total+=$ex;
    }
    array_map('total',$ex);
    echo $total;
}

Worked well, when you write normal integer without anything (for example - 3500) and double with a dot (for example - 3.5). I think this function is good enough to use it

Danielius
  • 852
  • 5
  • 23