0

I am using explode 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];
chris85
  • 23,846
  • 7
  • 34
  • 51
max
  • 353
  • 3
  • 4
  • 12

4 Answers4

2

Here are two approaches you could take. If you can change the comma separator to a period/full stop:

$data  = '10 x 2.5';
$floats = array_map('floatval', explode(' x ', $data));
echo $floats[0] * $floats[1];

Demo: https://3v4l.org/hFMEt

or you could use a regex which also will confirm you have the right data format.

echo preg_replace_callback('/(\d+(?:[.,]\d+)?)\h*x\h*(\d+(?:[.,]\d+)?)/', function($match) {
    return str_replace(',' , '.', $match[1]) * str_replace(',' , '.', $match[2]);
    }, $data);

PHP Demo: https://3v4l.org/I82Gt
Regex Demo: https://regex101.com/r/tTcJSp/1/

chris85
  • 23,846
  • 7
  • 34
  • 51
1

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];
Priyank
  • 470
  • 2
  • 11
  • This answer is very correct in the case that it had a decimal separated by ',' or by a '.' Thank you – max Aug 16 '17 at 04:38
  • This would also work in the case in the decimal east in the first position? For example: '7.5 x 10' or both '7.5 x 7.5' – max Aug 16 '17 at 05:05
0

try this:

<?php
 $data  = '10 x 2,5';
 $array = explode("x", $data);
 $total = floatval(str_replace(',', '.', $array[0]))*floatval(str_replace(',', '.', $array[1])); 
?>

here str_replace() is used to check if there are , in the string and then convert into its float value. Hope this helps.

  • Using the `str_replace` on the `$data` would save a couple calls, you then can map the `floatval` on the `$array`. (https://3v4l.org/nj1hi) – chris85 Aug 16 '17 at 04:26
  • @chris85 that is true. I kept it seperate considering there might a reason why there are `,` in the $data and exploding the with `x` is the first thing required. Thank You. – Gopalkrishna Narayan Prabhu Aug 16 '17 at 04:32
0
$data  = '10 x 2,5';
$array = explode("x", $data);

$decimal =$array[1];

//replace the comma with dot
$decimal_formated = str_replace(',', '.', $decimal);
//format the string to float
$decimal_formated = (float)$decimal_formated;
//OR $decimal_formated = floatval($decimal_formated);

//get total
$total = $array[0]*$decimal_formated;

//french format $total, with comma
$total_with_comma =number_format($total, 1, ',', ' ');


echo $decimal.'</br>'; //output 2,5
echo $decimal_formated.'</br>'; //output 2.5
echo $total.'</br>'; //output 25
echo $total_with_comma.'</br>'; // output 25,0

You can use number_format(), floatval().

Michael GEDION
  • 879
  • 8
  • 16