peter.o
peter.o

Reputation: 3530

Split string after one character or another

I have a file that contains many lines such as these:

2011-03-23  10:11:08    34  57  2   25,5    -
2011-03-23  10:11:12    67  54  3   3,5 -
2011-03-23  10:11:16    76  57  3   2,4 -
2011-03-23  10:11:18    39  41  2   25,5    +

Each line ends with + or -. I'd like the file content to be split after + or - sign. Lines doesn't have same number of characters.

I was trying to read the file using fgets() with auto_detect_line_endings on, but there were still many lines combined into single one:

Output example: Output should be two lines but there is only one (you can see the "the new line" but PHP doesn't):

2011-03-23 10:11:08 34 57 2 25,5 - 2011-03-23 10:11:12 67 54 3 3,5 -

EDIT:

Code I am using to read the file

ini_set('auto_detect_line_endings', true);
    $handle = fopen($filename, "r");
    $index = 1;
    if ($handle) {
        while (($line = fgets($handle)) !== false) {
            if (trim($line) != '') {
                $data = preg_split('/\s+/', trim($line));
                // Saving into DB...
                $index++;
            }
        }
    }
    fclose($handle);

Upvotes: 1

Views: 133

Answers (2)

h2ooooooo
h2ooooooo

Reputation: 39522

To make sure you get all of the possible new line combinations you should use preg_split instead:

LF = \n, CR = \r

LF: Multics, Unix and Unix-like systems (GNU/Linux, OS X, FreeBSD, AIX, Xenix, etc.), BeOS, Amiga, RISC OS and others.
CR: Commodore 8-bit machines, Acorn BBC, ZX Spectrum, TRS-80, Apple II family, Mac OS up to version 9 and OS-9
LF+CR: Acorn BBC and RISC OS spooled text output.
CR+LF: Microsoft Windows, DEC TOPS-10, RT-11 and most other early non-Unix and non-IBM OSes, CP/M, MP/M, DOS (MS-DOS, PC DOS, etc.), Atari TOS, OS/2, Symbian OS, Palm OS, Amstrad CPC

The regex would be /(\r\n|\n\r|\n|\r)/ (CR+LF or LF+CR or LF or CR):

$lines = preg_split('/(\r\n|\n\r|\n|\r)/', $string);

DEMO


If you plan on not having any empty lines (lines with white space count as empty) you can add an optional \s* to the end of your regex which will match 0 to an infinite amount of white spaces after your newlines:

$lines = preg_split('/(\r\n|\n\r|\n|\r)\s*/', $string);

DEMO


If you plan on not having any empty lines, but expect lines with white space to not count as empty, you can even simplify the regex:

$lines = preg_split('/[\n\r]+/', $string);

DEMO

Upvotes: 2

Parag Tyagi
Parag Tyagi

Reputation: 8960

TRY THIS:

<?php
  $input = "2011-03-23  10:11:08    34  57  2   25,5    -
          2011-03-23  10:11:12    67  54  3   3,5 -
          2011-03-23  10:11:16    76  57  3   2,4 -
          2011-03-23  10:11:18    39  41  2   25,5    +";

  // 1st explode by new line
  $output = explode("\n", $input);
  print_r($output);


  // 2nd remove last character
  $result = array();
  foreach($output as $op)
  {
     $result[] = substr($op, 0, -1);
  }
  print_r($result);


OUTPUT:

Array
(
   [0] => 2011-03-23  10:11:08    34  57  2   25,5    -
   [1] => 2011-03-23  10:11:12    67  54  3   3,5 -
   [2] => 2011-03-23  10:11:16    76  57  3   2,4 -
   [3] => 2011-03-23  10:11:18    39  41  2   25,5    +
)
Array
(
   [0] => 2011-03-23  10:11:08    34  57  2   25,5    
   [1] => 2011-03-23  10:11:12    67  54  3   3,5 
   [2] => 2011-03-23  10:11:16    76  57  3   2,4 
   [3] => 2011-03-23  10:11:18    39  41  2   25,5    
)


DEMO:
http://3v4l.org/0uIe7#v430

Upvotes: 1

Related Questions