Using sscanf()
instead of general-use string splitting functions like explode()
or preg_split()
, you can instantly data-type the isolated values.
For the sample string, using %f
twice will extract the space-delimited values and cast the two numeric values as floats/doubles.
Code: (Demo)
$pos = '98.9 100.2';
sscanf($pos, '%f %f', $number1, $number2);
var_dump($number1);
echo "\n";
var_dump($number2);
Output:
float(98.9)
float(100.2)
There is an alternative syntax which will act the same way, but return the values as an array. (Demo)
$pos = '98.9 100.2';
var_dump(sscanf($pos, '%f %f'));
Output:
array(2) {
[0]=>
float(98.9)
[1]=>
float(100.2)
}