PHP正则表达式获取其他数据

时间:2015-03-23 23:03:38

标签: php regex

我有以下格式的多行数据

<terminal:Text>1  #AY5767 F9 A9 P. J9 C9 D9 I9 Y9 LHRMIA 0945 1410   *  777 0E</terminal:Text>
<terminal:Text>        B9 H9 K9 M9 L9 V9 S9 N9 Q9 O9 G9 </terminal:Text>

终端:文本元素被加载并循环。目前我正在做

$elements = $dom->getElementsByTagNameNS('http://www.test.com/terminal', 'Text');
$elNum = 0;
while ( $elNum < $elements->length ) {

    $flightInfo = $elements->item($elNum)->nodeValue;

    if (preg_match('/^\\d/', $flightInfo) === 1) {

        if(preg_match('/(\d+[^#]*\#(\p{Lu}{2})\s*(\d{1,4})\b)(\s*\K[\w ]+(?=\s+\p{Lu}{6}))\s([A-Z]{3})([A-Z]{3})\s(.+)/', $flightInfo, $matches)===1){
            $fltcode = $matches[2].$matches[3];
            $ffrom = $matches[5];
            $fto = $matches[6];
            $other = $matches[7];

            $seatString = $matches[4];
            $seatInfo = explode( " ", trim( $seatString ) );

            $this->flights[$fltcode] = array(
                "flightNumber" => $fltcode,
                "from" => $ffrom,
                "to" => $fto,
                "seats" => $seatInfo,
                "other" => $other
            );
        }
    }
    ++$elNum;
}

它几乎捕获了第一行所需的所有数据。但是,第二行与第一行相关,第二行中的数据应添加到$ seatInfo。我在想类似的事情

while ($elNum + 1 < $elements->length && preg_match('/^\s*[A-Z][0-9\.\-]/i', $elements->item($elNum + 1)->nodeValue)) {
    ++$elNum;
    $seatInfo = trim($seatInfo) . " " . $elements->item($elNum)->nodeValue;
}

但它似乎并没有起作用。将第二行链接到$ seatInfo内容的最佳方法是什么?

由于

预期输出

$seatString = F9 A9 P. J9 C9 D9 I9 Y9 B9 H9 K9 M9 L9 V9 S9 N9 Q9 O9 G9

0 个答案:

没有答案