Conversion of .txt file to tabular form

Hi All,
(1). Please can anyone help me to use PHP or MySQL to automate the conversion and merging of the attached .txt files into a tabular format? I will paste the data into the quote below since I cannot attach the file here.
file 1 is named 2021_11_30.txt and the data there is below :

`----------NEW TRANSACTON-----------
TRANSACTION DATE : 08:42 AM, 30 Nov 2021
CLIENT ID : C00001
CLIENT NAME : SALISU BISI
AMOUNT : 16,450.00
TRANSACTION REF. : 00001

----------NEW TRANSACTON-----------
TRANSACTION DATE : 08:46 AM, 30 Nov 2021
CLIENT ID : C00002
CLIENT NAME : SULE YAYA
AMOUNT : 80,940.00
TRANSACTION REF. : 00002

----------NEW TRANSACTON-----------
TRANSACTION DATE : 08:51 AM, 30 Nov 2021
CLIENT ID : C00001
CLIENT NAME : SALISU BISI
AMOUNT : 44,900.00
TRANSACTION REF. : 00003

----------NEW TRANSACTON-----------
TRANSACTION DATE : 08:58 AM, 30 Nov 2021
CLIENT ID : C00003
CLIENT NAME : ADEGOKE BAYO
AMOUNT : 60,000.00
TRANSACTION REF. : 00004

----------NEW TRANSACTON-----------
TRANSACTION DATE : 09:12 AM, 30 Nov 2021
CLIENT ID : C00004
CLIENT NAME : COKER PRIYE
AMOUNT : 80,200.00
TRANSACTION REF. : 00005
`

file 2 is 2021_12_04.txt

----------NEW TRANSACTON-----------
TRANSACTION DATE : 09:42 AM, 04 Dec 2021
CLIENT ID : C00011
CLIENT NAME : SAINT OBI
AMOUNT : 100,450.00
TRANSACTION REF. : 00101

----------NEW TRANSACTON-----------
TRANSACTION DATE : 09:54 AM, 04 Dec 2021
CLIENT ID : C00112
CLIENT NAME : DESIREE MONICA
AMOUNT : 180,940.00
TRANSACTION REF. : 00102

----------NEW TRANSACTON-----------
TRANSACTION DATE : 09:58 AM, 04 Dec 2021
CLIENT ID : C00211
CLIENT NAME : ALIU MUSA
AMOUNT : 544,000.00
TRANSACTION REF. : 00103

----------NEW TRANSACTON-----------
TRANSACTION DATE : 10:40 AM, 04 Dec 2021
CLIENT ID : C00112
CLIENT NAME : DESIREE MONICA
AMOUNT : 860,000.00
TRANSACTION REF. : 00104

Blockquote

File 3. 2021_12_05.txt

`----------NEW TRANSACTON-----------
TRANSACTION DATE : 09:42 AM, 05 Dec 2021
CLIENT ID : C05011
CLIENT NAME : SAINT JOE PATRICK
AMOUNT : 700,550.00
TRANSACTION REF. : 02101

----------NEW TRANSACTON-----------
TRANSACTION DATE : 10:02 AM, 05 Dec 2021
CLIENT ID : C40112
CLIENT NAME : ABU SALE
AMOUNT : 180,650.00
TRANSACTION REF. : 02102
`
Please, how can I use PHP to convert the 3 files into tabular form?

What exactly do you mean by “tabular form”?

If by tabular form you mean a html-table, this should work:

```php
function readFileLine(string $fn)  
{
	$file = fopen($fn, 'r');
	while (($line = $file?fgets($file):false) !== false) 
	{
        yield $line;
	}
	if ($file)fclose($file);
}		


function fileToTableRows(string $fn) : string
{
	$openline = false;
	$ret = '';
	foreach (readFileLine($fn) as $line)
	{
		if ($line === '')
		{
			continue;
		}	
		if (strpos($line, '----------NEW TRANSACTON', )!==false)
		{
			if ($openline)$ret .= '</tr>';
			$ret .= '<tr>';
			$openline=true;
		}					
		else
		{
			$parts = explode(':', $line);
			$ret .= '<td>'.(count($parts) > 1?$parts[1]:$line).'</td>';
		}
	}	
	if ($openline)$ret .= '</tr>';
	return $ret;
}	

echo '<table>'
        .fileToTableRows('2021_11_30.txt')
        .fileToTableRows('2021_12_04.txt')
        .fileToTableRows('2021_12_05.txt')
       .'</table>';

Thanks very much, I have used file_getcontents() to read the files.

Sponsor our Newsletter | Privacy Policy | Terms of Service