php5从用空格分隔的字符串中复制子字符串


php5 copy sub-strings from string separated by space?

我使用的是php5,并且有一个返回客户端IP地址的脚本。使用shell_exec()函数执行脚本。现在的输出是这样的:*192.168.10.40 192.168.10.41*。现在我需要将其存储在一个数组中。我使用了preg_match(),但它不起作用。

以下是使用preg_match()的代码:

$test = shell_exec("/www/dhcp.sh");
$pattern='/([^ ]*) /';
preg_match($pattern, $test, $new);

preg_match()返回0;

这是我使用explode()的一个:

$test = shell_exec("/www/dhcp.sh");
var_dump( explode(' ', $test ) );

我也使用了爆炸,但我得到的结果是:array(1){[0]=>string(28)"192.168.10.40 192.168.10.41"}有人能告诉我如何将字符串拆分为数组吗?

问候,
Sowmya

您可以使用爆炸来拆分字符串:

explode(' ', '192.168.10.40 192.168.10.41'));

这会给你

array(2) {
  [0]=>
  string(13) "192.168.10.40"
  [1]=>
  string(13) "192.168.10.41"
}

http://php.net/manual/fr/function.explode.php