0

I've spent the better part of 2 days searching on all the ways to split strings in PHP, but none of them got me anywhere. I need to be able to split the following string format into arrays, and then split each of those arrays into sub-arrays.

The data is a journal of register sales. Each transaction is in the same format and the only difference in string length per transaction is the amount of items purchased. Here is an example: (This receipt is one single hex stream, all empty spaces are a space character (\x20) and all '.' are (\x00). Again, these are all 1 single line just edited to make it easier to look at)

    DATE           07/13/2014            MON...
         PLU#491320                          ..
          SPRITE T1                    $0.75...
          CUSTOMER DATE OF BIRTH  02/05/1978...
         PLU#820050                          ..
          SMIRNOFF VODKA LT T2        $13.99...
          TAX1 AMT                     $0.06...
          TAX2 AMT                     $1.42...
          .T.O.T.A.L              .$.16..2.2...
          .C.H.A.R.G.E            .$.16..2.2...
    NO.000002 REG01 EMPLOYEE 2    TIME 18:09..€

Basically I'm looking for:

    Array {
           [0] = Date: 07/13/2014
           [1] = PLU#/Tax/Price: 491320 & T1 & 0.75, 820050 & T2 & 13.99 (no spaces)
           [2] = Tax: T1 & 0.06, T2 & 1.42 (no spaces)
           [3] = Transaction No.: 000001
           [4] = Register: 01
           [5] = Employee: Employee 2
           [6] = Time: 17:59
    }

I've tried preg_replace, preg_split, substr and str_split, using strpos and searching for the string position for each section. Nothing worked. I can clear the spaces, but it screws up with stuff like Employee name, which may have an actual space in it.

Are there any other ways of splitting strings besides what I just listed? I've tried dozens of substr and preg functions and none of them helped me. I've spent enough time with trial and error from google searches, I may as well ask if someone knows something I haven't come across.

user3838726
  • 11
  • 1
  • 3

1 Answers1

0

You can split it with "\n" to get lines, then trim excessed spaces and with regex get left and right parts.

EDIT: I just realized that you dont want named array for left and right collumn. You just wanted split based on lines.

explode("\n", $input) //double quotes are necessary
Keo
  • 1,143
  • 8
  • 19
  • This kind of worked, this also made me realize that I forgot to mention these aren't in "lines" but rather one single stream of hex. This split each transaction after the first / in the date, so rather than the next line starting with DATE, the line before ended with DATE07. – user3838726 Jul 15 '14 at 00:09
  • Then post sample in original form, well figure something out. Btw did you try [chunk-split](http://php.net/manual/en/function.chunk-split.php)? – Keo Jul 15 '14 at 00:15