Help me parse this file with PHP

后端 未结 4 1029
囚心锁ツ
囚心锁ツ 2021-01-28 17:13
Fri Nov 27 10:00:01 EST 2009         974        12506
Fri Nov 27 11:00:01 EST 2009         988        12655
Fri Nov 27 12:00:01 EST 2009        1005        12886
Fri Nov 27 1         


        
4条回答
  •  逝去的感伤
    2021-01-28 17:43

    $lines = explode( "\r\n", $str );
    
    foreach( $lines as $line){
    
    echo 'date = ' .  substr( $line, 0 , 28) . PHP_EOL;
    echo 'var1 =  ' . substr( $line, 28 , 12) . PHP_EOL;
    echo 'var2 =  ' . substr( $line, 40 , 12) . PHP_EOL;
    echo '
    ' ; }

    If possible PHP treats all incoming vars as an integer first of all, so if the text files are of predictable length ( 24, 12, 12 Chars that I can see) then 0000000974 will be correctly parsed as being the integer 974 without having to resort to trim.

    Isn't it alway best to aviod regexes if you can?

提交回复
热议问题