我一直在使用混合了strpos、substr和regex的PHP来处理大量的字符串,但我一直觉得它不够完美。我正在玩的字符串看起来像这个
string(63) "1*O#AY5523 F7 A5 J9 C9 D8 I1 W9 E4 LHRLAX-0935 1245 * 388 0E"
string(63) "2*O#BA 3 F9 A9 J9 C9 D9 R9 I4 W9 LHRLAX-0935 1245 388 0E"
string(27) "US7080 INTL ONL CNX/STP TFC"
我试图获得的数据是
$this->terminalData[] = array(
"flightNumber" => $fltcode,
"from" => $ffrom,
"to" => $fto,
"seats" => $seats,
"other" => $flightInfo
);
这些都是我所知道的。
会有很多数据字符串,但我需要的行总是以数字开头(所以上面的前两个字符串我想要,第三个字符串没有以数字开头,所以可以忽略)
航班号总是有一个在前的#。这是我当前代码中偶尔出错的部分,因为我使用了strpos,但我不知道flightNumber的长度。航班号总是有2个字母,后面跟着1-4个数字。字母和数字之间可能有空格。所以在我上面的两个字符串中,航班号是AY5523和BA3。
From和To总是在一起,由六个大写字母组成(前三个From,后三个To)。在上面的字符串中,从是LHR,到是LAX。
座位总是一个字母/数字组合,后面跟着一个空格,所以上面都是F7 A5 J9等。
其他是从和到之后的所有附加数据,例如US7080 INTL ONL CNX/STP TFC
//Get the String of data
$flightInfo = $elements->item($elNum)->nodeValue;
//Does it start with a digit?
if ( preg_match('/^'d/', $flightInfo ) === 1 )
{
$pat = strpos($flightInfo, "#");
$fltcode = substr($flightInfo, $pat+1, 6);
$fltcode = str_replace(' ', '', $fltcode);
$flightInfo = substr( $flightInfo, $pat+6 );
$seatInfo = preg_replace('/[A-Za-z]{6,6}.*$/i', '', $flightInfo);
$flightInfo = str_replace( $seatInfo, "", $flightInfo );
$ffrom = substr( $flightInfo, 0, 3 );
$fto = substr( $flightInfo, 3, 3 );
$flightInfo = substr( $flightInfo, 6 );
while ( $elNum+1 < $elements->length && preg_match('/^'s*[A-Z][0-9'.'-]/i', $elements->item($elNum+1)->nodeValue))
{
$seatInfo = trim($seatInfo) . " " . $elements->item($elNum)->nodeValue;
}
$seatInfo = explode( " ", trim( $seatInfo ) );
$seats = array();
foreach ( $seatInfo as $si )
{
$seats[ substr( $si, 0, 1) ] = (int) substr( $si, 1 );
}
$this->terminalData[] = array(
"flightNumber" => $fltcode,
"from" => $ffrom,
"to" => $fto,
"seats" => $seats,
"other" => $flightInfo
);
}
我想它就快到了,只需要一点帮助,确保它总是有效的。我目前的主要问题是如何将$flightInfo作为大小为6的子字符串,因为我不能保证航班号会是这个长度。
因此,实际上我的目标是使用更多的regex而不是substr等。我也很感兴趣的是,是否有一种更好的方法可以将字符串拆分为我需要的数据。
任何关于事情的建议都很感激。
感谢
这就是我要做的:
$str = '1*O#AY5523 F7 A5 J9 C9 D8 I1 W9 E4 LHRLAX-0935 1245 * 388 0E';
$reg = '~'d.+#([A-Z]{2}'s?[0-9]{1,4})'s(.+)'s([A-Z]{6})-([0-9]{4}'s[0-9]{4})~';
preg_match($reg, $str, $matches);
$flight_no = $matches[1];
$seat_no = explode(' ', trim($matches[2]));
$from = substr($matches[3], 0, 3);
$to = substr($matches[3], 3, 3);
var_dump($flight_no);
var_dump($seat_nos);
var_dump($from);
var_dump($to);
您应该能够获取"其他"数据。
解释
~ # opening delimiter
'd.+ # match digit followed by any character one or more times
'# # match hash sign #
( # opening capture parentheses for flight info
[A-Z]{2} # match 2 uppercase letters
's? # match space zero or one time
[0-9]{1,4} # match 4 digits consecutively
) # closing capture parentheses for flight info
's # match single whitespace
(.+) # capture everything till space & uppercase char encountered
's # match single whitespace
( # opening capture parentheses for from/to info
[A-Z]{6} # capture 6 upercase letters, from/to
) # closing capture parentheses for from/to info
- # match hypen which seperates from/to from time
( # opening capture parentheses for time info
[0-9]{4} # match 4 digits, depart time
's # match single whitespace
[0-9]{4} # match 4 digits, arrival time
) # closing capture parentheses for time info
~x # closing delimiter with free-space modifier 'x'
注意,我使用了自由间距来注释带有"x"修饰符的正则表达式,因此必须在正则表达式中转义哈希符号