Search code examples
phpsplitfloating-pointexplodetext-parsing

Parse string containing two numbers and assign them to two variables


$pos contains two numbers delimited by a space in one string.

$pos = 98.9 100.2

How can I split this into 2 variables? I obviously have to check for the space in between.

I would like to have two variables afterwards:

$number1 = 98.9
$number2 = 100.2 

Solution

  • list($number1, $number2) = explode(' ', $pos);
    

    However, make sure the string has the right format before doing this.