1

I am trying to get data of all transactions from a file which contains multiple transaction data, it happens so that I can get transaction data of one transaction by php explode. So my question is how to explode the data to get multiple transactions data ..

$data['file'] = file_get_contents('C:\TSaveBatchDBMessage.txt');
    $data['exp'] = explode('TSaveTransactionMessage',$data['file']);

it only gives output for the first transaction data, and one files contains almost dozens of transactions ..

Help appreciated

PS: This file is generated by software and there is no possible count of transactions in the file, they may be 100, 1 or any number in between ..

Numan
  • 127
  • 8
  • 1
    Read the file [line by line](https://stackoverflow.com/questions/13246597/how-to-read-a-large-file-line-by-line) and explode as you do now. – Andrei Nov 07 '19 at 09:12
  • Brother .. the transactions are uncountable sometimes .. because the file is created again and again, if that was not the case .. i would have – Numan Nov 07 '19 at 09:15
  • The read the whole file into an array and parse it key by key. – Andrei Nov 07 '19 at 09:15
  • i tried to run it in a foreach loop and if the value of a key is equal to 'TSaveTransactionMessage', I tried to echo there, but its not working – Numan Nov 07 '19 at 09:50

2 Answers2

2

Read the file as an array, iterate through and process each line...

foreach( file('C:\TSaveBatchDBMessage.txt') as $line ){
    $data['exp']=explode( 'TSaveTransactionMessage', $line );

    /* do stuff with array */
}
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
  • its not helping because the $data['exp'] is not repeating .. instead it gives only first value .. – Numan Nov 07 '19 at 09:49
  • it worked when i put a variable with 0 and incremented it in foreach loop and put that in as $data[variable]. its pretty close to what i need .. thank you – Numan Nov 07 '19 at 09:54
  • ok - to continually add to that variable use `$data['exp'][]=explode( 'TSaveTransactionMessage', $line );` instead – Professor Abronsius Nov 07 '19 at 10:00
0

First read the file line by line than check its count is grater than 1 use foreach loop else do as you are doing

 $data['file'] = file_get_contents('C:\TSaveBatchDBMessage.txt');
    if(sizeof($data['file']) > 1){
    foreach($data['file'] as $file){
    $data['exp'] = explode('TSaveTransactionMessage',$file);
    }
    }else{
     $data['exp'] = explode('TSaveTransactionMessage',$data['file']);
    }
Mannu saraswat
  • 1,061
  • 8
  • 15