如何读取部分输入


How to read only part of input

我正试图弄清楚如何从eve-online kill邮件中读取数据库所需的几行:

2011.12.30 23:26

受害者:Annabel Lust
军团:暗影巨石
联盟:xXDEATHXx之影
派系:没有
毁:猎豹
系统:C6CG-W
安全:0.0
伤害:827

涉及政党:

姓名:Milena Dush
安全:0.6
公司:nXo
联盟:无畏渡口
派系:没有
船:猎犬
武器:加达里海军毒药鱼雷
伤害:457

摧毁物品:

姐妹扩展探针启动器
催化冷-气电弧推进器
纳米纤维内部结构II
超速喷油器系统II
硬接线- genution核心增强CA-2 (Cargo)
纳米工程(货物)
姐妹芯扫描探针,数量:5个(货物)
微型辅助电源芯I (Cargo)
姐妹深空扫描探测器,数量:5个(货物)

删除条目:

秘密行动隐形装置II
姐妹战斗扫描探针,数量:5个
翘曲干扰器II
纳米纤维内部结构II
天文精确定位(货物)
小型重力电容器升级1,数量:2(货物)
一级救助员(货物)
拦截器(货物)
热力学(货物)
副处理机I(货物)

当用户在多文本框中输入killmail时,我只需要读取销毁的物品和丢弃的物品,并将数量放在单独的字符串中。

有办法吗?

可以使用explosion (separator, text)将文本分割为两个数组。例子:$ str =爆炸(":"、"YourText");

<?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为数量。