简体   繁体   English

PHP正则表达式创建数组

[英]PHP Regular expression to create an array

I have a string which is pulled from iCal, which outputs the description. 我有一个从iCal提取的字符串,该字符串输出描述。 I want to take the description each time and create an array with the objects I need. 我想每次进行描述,并使用所需的对象创建一个数组。 They always follow the same pattern. 它们始终遵循相同的模式。

For example I have: 例如,我有:

Description: Trip status: Confirmed\n \n FLIGHT INFORMATION: \n \n United Airlines UA 485: \n \n \n From: Newark Liberty International (New York, USA) - TerminalC at Fri, Sep 28, 2012 16:34 (local time) \n To: Denver International (Denver, USA) at Fri, Sep 28, 2012 18:47 (local time) \n Cabin: Economy Restricted\n Duration: 04:13\n Stop(s): 0\n Aircraft: Boeing 757-200\n

and then 接着

Description: Trip status: Confirmed\n \n FLIGHT INFORMATION: \n \n Air Canada AC 1072: \n \n \n From: Denver International (Denver, USA) at Sat, Sep 29, 2012 10:55 (local time) \n To: P Trudeau International (Montreal, Canada) at Sat, Sep 29, 2012 16:29 (local time) \n Cabin: Economy Restricted\n Duration: 03:34\n Stop(s): 0\n Aircraft: Airbus Industrie A319\n

As you can see the different outputs follow the same structure, so in an ideal world I would want: 如您所见,不同的输出遵循相同的结构,因此在理想的世界中,我想要:

$itinery[0]: Confirmed
$itinery[1]: United Airlines UA 485
$itinery[2]: Newark Liberty International (New York, USA)

Any guidance as ever is appreciated. 任何指导,如感激。

Thanks. 谢谢。

You could also break each line apart with explode() 您还可以使用explode()将每一行分开

$parts = explode ( $line, '\n' ) ;
print_r ( $parts ) ;

Then break the parts with explode ( $part[$i], ':' ) to get the values. 然后用explode($ part [$ i],':')断开部分以获取值。

A more specific regex : 更具体的正则表达式:

Description:\\s*Trip\\s+status:\\s+(\\w+)\\s*FLIGHT\\s+INFORMATION:\\s*([^:]+):\\s*From:\\s*([\\w\\d_,\\s-()]+)\\s+at\\s+(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat), 说明:\\ s *旅行\\ s +状态:\\ s +(\\ w +)\\ s *飞行\\ s +信息:\\ s *([^:] +):\\ s *来自:\\ s *([\\ w \\ d _,\\ s-()] +)\\ s + at \\ s +(?: Sun | Mon | Tue | Wed | Thu | Fri | Sat),

Following assumptions are made : 做出以下假设:

  • The "status" can contain only letters. “状态”只能包含字母。
  • The "flight information" is supposed to end with a : 在“航班信息”应该有结束:
  • The "from" can contain only letters, numbers, one of those characters _,-() or blank characters. “ from”只能包含字母,数字, _,-()或空白字符。 The "from" is immediately followed by this sequence : " at XXX" where XXX is a day (ie Sun,Mon,Tue,Wed,Thu,Fri or Sat). “ from”后面紧跟着以下顺序:“ at XXX”,其中XXX是一天(即星期日,星期一,星期二,星期三,星期四,星期五或星期六)。

Tips : 提示 :
If status belongs to a closed list then replace (\\w+) with (?:Confirmed|Cancelled|Delayed) in the regex. 如果状态属于封闭列表,则在正则表达式中将(\\w+)替换为(?:Confirmed|Cancelled|Delayed) Add other statuses if necessary. 如有必要,添加其他状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM