2

PHP が初めてで、ドキュメントなどを見ても、この質問に対する答えが見つかりません。

次のような入力を取りたい$_POST

Large Automated Structural Restoration  1   Hull Repair Unit        Medium  50 m3
Experimental 10MN Microwarpdrive I  5   Propulsion Module       Medium  50 m3
Warp Disruptor I    1   Warp Scrambler      Medium  5 m3
Upgraded EM Ward Amplifier I    1   Shield Amplifier        Medium  5 m3
Tracking Disruptor I    1   Tracking Disruptor  Small   Medium  5 m3

好きarrayに:

[Experimental 10MN Microwarpdrive I] [5] [Propulsion Module] [] [Medium] [50] //Disregard m3
[Warp Disruptor I] [1] [Warp Scrambler] [] [Medium] [5]
...
[Tracking Disruptor I] [1] [Tracking Disruptor] [Small] [Medium] [5]

$asset[0][name]外部リソースへの XML 呼び出しを準備できるように、変数を呼び出すことができる場所まで。

ロジックが私を逃れているか、何かを理解していません。助けてください!

4

2 に答える 2

1
$aero=explode(PHP_EOL,trim($_POST['textarea'])); //separate each line
$asset=array(); //init the assets
foreach ($aero as $unit) { //loop each line
    $detail=explode("\t",$unit); //split by tab
    $name=$detail[0]; //assign the name to the first item in the arr
    unset($detail[count($detail)-1]); //delete the last item ('m3' not needed)
    unset($detail[0]); //delete the first item (we saved it as $name)
    $asset[][$name]=$detail; //add an array item
}

楽しみのために、タブがない場合に正規表現の 1 ライナーを使用する別のソリューションを次に示します。

$regex='/^([A-Za-z0-9 ]+) (\d+) ([A-Z][A-Za-z ]+?)(\ ()|\ (Small)\ )([A-Z][a-z]+) (\d+) m3$/m';
preg_match_all($regex,$textarea,$aero);
$asset=array();    
foreach ($aero[1] as $no=>$unit) {
    $asset[$unit]=array($aero[2][$no],
                $aero[3][$no], 
                $aero[6][$no], 
                $aero[7][$no], 
                $aero[8][$no]); 
}

おそらく、このビットには小さな追加が必要になるでしょう(\ ()|\ (Small)\ ):(\ ()|\ (Small)\ |\ (Medium)\ |\ (Large)\ )

処理前の例の最初と最後の行の正規表現出力:

Array ( [0] => Array ( [0] => Large Automated Structural Restoration 1 Hull Repair Unit Medium 50 m3 [1] => Tracking Disruptor I 1 Tracking Disruptor Small Medium 5 m3 )  
[1] => Array ( [0] => Large Automated Structural Restoration [1] => Tracking Disruptor I )  
[2] => Array ( [0] => 1 [1] => 1 )  
[3] => Array ( [0] => Hull Repair Unit [1] => Tracking Disruptor )  
[4] => Array ( [0] => [1] => Small )  
[5] => Array ( [0] => [1] => )  
[6] => Array ( [0] => [1] => Small )  
[7] => Array ( [0] => Medium [1] => Medium )  
[8] => Array ( [0] => 50 [1] => 5 ) )
于 2013-02-02T20:57:36.950 に答える
0

>1空白の正規表現の区切り文字を含む文字列の場合explode、すべてが配列になり、必要なことを行うことができます。

$reg = '\s+';
$ar = explode($reg,$_POST['val']);
于 2013-02-02T20:26:26.843 に答える