将给定的String拆分为两个变量 - php

时间:2010-12-04 11:58:16

标签: php variables filter

我得到了变量$ pos,它在一个字符串中包含两个数字:

$pos = 98.9 100.2

如何将其分为2个变量?我显然必须检查两者之间的空间。 之后我想要两个变量:

$number1 = 98.9
$number2 = 100.2 

3 个答案:

答案 0 :(得分:14)

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

但是,在执行此操作之前,请确保字符串格式正确。

答案 1 :(得分:1)

您可以使用:

$pos = "98.9 100.2";
$vals = preg_split("/[\s]+/", $pos);
list($number1, $number2) = $vals;

答案 2 :(得分:0)

如果它始终是空格,请检查

array explode ( string $delimiter , string $string [, int $limit ] )

在你的情况下,你有

$foo = "1.2 3.4 invalidfoo"
$bits = explode(" ",$foo);

给你一个数组:

echo 0+$bits[0];    
echo 0+$bits[1];
echo 0+$bits[3];

使用+0强制施放:)