max max -3 years ago 116
PHP Question

Separate string with explode php

I am using

in PHP, because I need to separate a string. of this type:

$Data = '10 x 4';

I need to separate the numbers because then I do a math operation on them . The code I have works well when they are integers. If I have a string of this type
10 x 2.5
it does not take into account the decimal part that would be the
, 5
. How can I do to make me take this part into account too?

$data = '10 x 2,5';
$array = explode("x", $data);
$total = $array[0]*$array[1];

Answer Source

Try with this. Just replace "," with "." and it's working.

$data  = '10 x 2,5';
$data  = str_replace(',','.','10 x 2,5');
$array = explode("x", $data);
echo $total = $array[0]*$array[1];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download