Splitting given String into two variables - php

11,782

Solution 1

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

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

Solution 2

You could use:

$pos = "98.9 100.2";
$vals = preg_split("/[\s]+/", $pos);
list($number1, $number2) = $vals;
Share:
11,782
algro
Author by

algro

Interaction and Interface Designer

Updated on June 17, 2022

Comments

  • algro
    algro almost 2 years

    I got the variable $pos, which contains two numbers in one String:

    $pos = 98.9 100.2
    

    How can I split this into 2variables? I oubviously have to check for the space in between. I would like to have two varibles afterwards:

    $number1 = 98.9
    $number2 = 100.2