<?php
$str = " 2011.12.30 23:26
Victim: Annabel Lust
Corp: Shadow Monolith
Alliance: Shadow of xXDEATHXx
Faction: None
Destroyed: Cheetah
System: C6CG-W
Security: 0.0
Damage Taken: 827
Involved parties:
Name: Milena Dush
Security: 0.6
Corp: nXo
Alliance: Intrepid Crossing
Faction: None
Ship: Hound
Weapon: Caldari Navy Bane Torpedo
Damage Done: 457
Destroyed items:
Sisters Expanded Probe Launcher
Catalyzed Cold-Gas Arcjet Thrusters
Nanofiber Internal Structure II
Overdrive Injector System II
Hardwiring - Genolution Core Augmentation CA-2 (Cargo)
Nanite Engineering (Cargo)
Sisters Core Scanner Probe, Qty: 5 (Cargo)
Micro Auxiliary Power Core I (Cargo)
Sisters Deep Space Scanner Probe, Qty: 5 (Cargo)
Dropped items:
Covert Ops Cloaking Device II
Sisters Combat Scanner Probe, Qty: 5
Warp Disruptor II
Nanofiber Internal Structure II
Astrometric Pinpointing (Cargo)
Small Gravity Capacitor Upgrade I, Qty: 2 (Cargo)
Salvager I (Cargo)
Interceptors (Cargo)
Thermodynamics (Cargo)
Co-Processor I (Cargo)
";
var_dump(parseItems('/Destroyed items:(?P<destroyedItems>.*)Dropped items:/is', $str, 1));
var_dump(parseItems('/Dropped items:(?P<droppedItems>.*)/is', $str, 1));
function parseItems($regex, $str, $defaultQty = 0){
preg_match($regex, $str, $match);
$lines = explode("\n", $match[1]);
$defaultQty = 0;
$items = array();
foreach($lines as $key => $line){
$line = trim($line);
if($line != ""){
$quantity = (strpos($line, "Qty:") != false) ? getQty($line) : $defaultQty;
$items[] = array($line => $quantity);
}
}
return $items;
}
function getQty($line){
preg_match('/, Qty:\s+(?P<quantity>[0-9]+)/', $line, $match);
return $match['quantity'];
}
這應該會給你一個數組的丟棄和毀壞行數。它返回關聯數組,其中key是行,value是數量。
正則表達式,簡單明瞭。 – Cyclone 2011-12-31 00:14:03
做無數量的項目是指數量是1嗎? – alex 2011-12-31 00:23:15
[這裏是開始](http://codepad.viper-7.com/7SNbmH)。 – alex 2011-12-31 00:38:50