この小さなスクリプトは、あなたの注文を多次元配列に解析します(下の出力を参照)。 この配列からは、データベースに挿入したり、他のファイルに書き込んだりするなど、他の形式を生成することができます。
パーサ
$orders = explode("BREAK", $fileContents);
array_shift($orders); // only required if a "BREAK" is before the first order
$ordersData = array();
foreach($orders as $order) {
if (preg_match("/Order: ([^\r\n]+)[\r\n]+NAME: ([^\r\n]+)[\r\n]+Phone: ([^\r\n]+)[\r\n]+(.+)/si", $order, $regs)) {
$items = preg_split("/[\r\n]+/", $regs[4], null, PREG_SPLIT_NO_EMPTY);
foreach ($items as &$item) {
if (preg_match("/(\d+) *?x *?(.+)/", $item, $regs2)) {
$item = array(
'count' => $regs2[1],
'item' => $regs2[2]
);
} else {
$item = array(
'count' => 1,
'item' => $item
);
}
}
$orderData = array(
'order_no' => $regs[1],
'name' => $regs[2],
'phone' => $regs[3],
'items' => $items
);
$ordersData[] = $orderData;
}
}
出力形式
$ordersData = Array
(
[0] => Array
(
[order_no] => 10000
[name] => XYZ
[phone] => XXX-XXX-XXX
[items] => Array
(
[0] => Array
(
[count] => 6
[item] => ABC
)
[1] => Array
(
[count] => 6
[item] => Main ST
)
[2] => Array
(
[count] => 6
[item] => CITY
)
[3] => Array
(
[count] => 6
[item] => STATE
)
[4] => Array
(
[count] => 6
[item] => COUNTRY
)
)
)
[1] => Array
(
[order_no] => 10001
[name] => XYZ1
[phone] => XXX-XXX-XXX
[items] => Array
(
[0] => Array
(
[count] => 1
[item] => BMC
)
[1] => Array
(
[count] => 1
[item] => MJ ST
)
[2] => Array
(
[count] => 1
[item] => CITY
)
[3] => Array
(
[count] => 1
[item] => STATE
)
[4] => Array
(
[count] => 1
[item] => COUNTRY
)
)
)
[2] => Array
(
[order_no] => 10002
[name] => XYZ2
[phone] => XXX-XXX-XXX
[items] => Array
(
[0] => Array
(
[count] => 1
[item] => BMC
)
[1] => Array
(
[count] => 1
[item] => MJ ST
)
[2] => Array
(
[count] => 1
[item] => CITY
)
[3] => Array
(
[count] => 1
[item] => STATE
)
[4] => Array
(
[count] => 1
[item] => COUNTRY
)
)
)
)
は、あなたはちょうどあなたが何をしたいのサンプル出力を追加することができます... – Baba
しかし私はBREAK BREAKタグの間のファイルにスライスこの情報が必要最後ではないので、ファイルの最後まで読み込むべきです。 –