0

ユーザーが1つの文字列にさまざまなオプションを入力できる単一のフィールドがあります。したがって、13123123|540|450

これらの 3 つの値を 3 つの変数に解析するにはどうすればよいでしょうか?

4

3 に答える 3

1

listを使用して、それらを 3 つの異なる変数に入れることができます。

$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 に答える