这个小脚本将您的订单解析为一个多维数组(见下面的输出)。从这个数组中,您可以生成任何其他格式,例如将其插入数据库、将其写入其他文件或其他任何内容。
解析器
$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
)
)
)
)