0

我有一个字段,允许用户在一个字符串中输入不同的选项。所以,13123123|540|450

我如何将这三个值解析为三个变量?

4

3 回答 3

1

您可以使用list将它们放入三个不同的变量中:

$str = '13123123|540|450';
list($one, $two, $three) = explode('|', $str);

或者,如果您愿意,您可以通过数组索引访问它们:

$str = '13123123|540|450';
$split = explode('|', $str);
// $split[0] == 13123123
于 2012-04-21T21:46:10.313 回答
1

您可以尝试以下方法:

$input = @$_POST["field"];

//  Method 1: An array

$options = explode ("|", $input);

/*
    The $options variable will now have the following:
    $options[0] = "13123123";
    $options[1] = "540";
    $options[2] = "450";
*/

// Method 2: Assign to different variables:

list($opt1, $opt2, $opt3) = explode ("|", $input);

/*
    The variables will now have the following:
    $opt1 = "13123123";
    $opt2 = "540";
    $opt3 = "450";
*/

// Method 3: Regular expression:

preg_match ("/(\w*)|(\w*)|(\w*)/i", $string, $matches);

/*
    The $options variable will now have the following:
    $matches[0] = "13123123";
    $matches[1] = "540";
    $matches[2] = "450";
*/
于 2012-04-21T21:49:31.700 回答
0

根据每个|. 所以类似于[\d{8}]^\|]第一个,依此类推。

于 2012-04-21T21:46:39.193 回答