dongshun1884 2014-07-07 13:20
浏览 19

用PHP将字符串拆分为数组

how would i go about splitting the following format string into an array?

[13:05:37] [Server thread/INFO]: [0;35;1m[Server[0;35;1m] hi[m

the string format is from a mine craft server and i believe the format is

[time] [thread info]: ANSICOLOROPEN[stringANSICOLORCLOSE] string EOL

how could i split this so the array would be

[0]time 
[1]thread info
[3]ansi open
[4]string
[5]ansi close
[6]string 
[7]EOL marker
  • 写回答

2条回答 默认 最新

  • doudu3961 2014-07-07 14:11
    关注

    I tend to start by exploding these types of log entries on the spaces and then hard-code in the index values since those tend not to change. For example:

    $log_line = "[13:05:37] [Server thread/INFO]: [0;35;1m[Server[0;35;1m] hi[m";
    $line_parts = explode(" ",$log_line);
    
    $log_data = array(
     'time' => $line_parts[0],
     'thread_info' => $line_parts[1],
     'somethingansirelated' => $line_parts[2]
    );
    

    It's much easier to further clean-up your various log datas from here in a variety of ways, for example you could use array_map:

    array_map( function( $a ){
      // trim, format, validate, etc.
    
    }, $log_data );
    

    Not sure if this will get mauled as a non-answer or not but it might help, so here you go.

    评论

报告相同问题?

悬赏问题

  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作