2012-04-21 9 views
0

私は、ユーザーが1つの文字列に異なるオプションを入力できる単一のフィールドを持っています。したがって、13123123 | 540 | 450テキストを特殊文字で区切って解析しますか?

3つの値を3つの変数にどのように解析すればよいですか?

+0

あなたは「特異的」または「任意」を意味していましたか? –

答えて

1

次の3つの異なる変数にそれらを置くためにlistを使用することができます:あなたが望んでいた場合は代わり

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

、あなただけの配列なインデックスを経由してアクセスすることができますする:

$str = '13123123|540|450'; 
$split = explode('|', $str); 
// $split[0] == 13123123 
0

使用正規表現に基づいて、それぞれの前の数字は|です。だから最初のもののように[\d{8}]^\|]の行に沿って何か。

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"; 
*/ 
関連する問題