我有单个字段,允许用户在一个字符串中输入不同的选项。那么,13123123 | 540 | 450
我如何将这三个值解析为三个变量?
答案 0 :(得分:1)
您可以使用list将它们分成三个不同的变量:
$str = '13123123|540|450';
list($one, $two, $three) = explode('|', $str);
或者,您可以通过阵列标记访问它们,如果您想:
$str = '13123123|540|450';
$split = explode('|', $str);
// $split[0] == 13123123
答案 1 :(得分: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";
*/
答案 2 :(得分:0)
根据每个|
之前的数字使用正则表达式。因此,对于第一个问题,[\d{8}]^\|]
符合要求,依此类推。