Urgent help request regarding TEXT FILE MANIPULATION with PHP

Hello everybody - and thanks for fast reply.

Well, I have the following need: Once a month I receive a text file with the following content (per line/line content with fixed lenght but different values):

00000035412012013100000000000000000120120131000000000 00000000000000000000000000+0000000002012010000000000200000000000171753-00000000000000000+00000000000000000+00000000000171753-000000000000000000000000000000000000000000+00000000000000000+00000000000000000+00000000000000000+00000000000000000000000LOHN/01 LG 000000000000000000000000000000000000000000000000000+000000000000000000000000000000+000000000000000000000000 0000000000000000000000 0*

This is only one line with 480 digits. I do have to cut the line(s) into certain slices (with different lenght) and write it to a text file (example .csv) with “;” as separator.

That way it will look like that: 000000;354120120131; … and so on.

How can I do so?

Thanks a lot for your help!

Regards

SAM

Sam,

We can help, but, help us help you… LOL…

First, you have 480 characters, mostly numbers. How do you know where the breaks are for your data?
If they are zeros, then we can create a script for you. If they are set fields, that can be done, too.

In your sample:
“That way it will look like that: 000000;354120120131; … and so on.”
I would have guessed that was one large number starting with zeros. Why/how did you decide to break it at 6 zeros, then numbers? Let us know how you want it broke up and then we can figure out some code for you.

Oh, thank you so much for writing!

Well, I have now collected some code (with support of others :D), that looks like this one:

<?php $fh=fopen('uploads/datei.txt'); while(!feof($fh)){ $string = fgets($fh); $array[1].=substr($string,2,9); $array[2].=substr($string,19,9); $array[3].=substr($string,28,9); $array[4].=substr($string,37,8); $array[5].=substr($string,45,9); $array[6].=substr($string,99,5); $array[7].=substr($string,104,2); $array[8].=substr($string,108,18); $array[9].=substr($string,300,18); } $new_variable = implode(';',$array); echo $new_variable; ?>

What I have to do now, is to write these code slices into my data base (because currently it is only working for ONE line of 480 charakters), that has the following structure:

mysql_query(“CREATE TABLE datenimport (
id int(255) NOT NULL auto_increment,
kennung varchar(1) NOT NULL default ‘0’,
zusatzkennung varchar(1) NOT NULL default ‘0’,
buchungsdatum varchar(100) NOT NULL,
beleg varchar(8) NOT NULL,
buchungstext varchar(60) NOT NULL,
sollkonto varchar(100) NOT NULL,
habenkonto varchar(100) NOT NULL,
buchungsbetrag varchar(100) NOT NULL,
steuerbetrag varchar(100) NOT NULL,
steuersatz varchar(100) NOT NULL,
fwcode varchar(100) NOT NULL default ‘0’,
fwbuchungsbetrag varchar(100) NOT NULL,
fwsteuerbetrag varchar(100) NOT NULL,
nettozahlungsziel varchar(100) NOT NULL,
skontotage1 varchar(100) NOT NULL,
skonto1 varchar(100) NOT NULL,
skontotage2 varchar(100) NOT NULL,
skonto2 varchar(100) NOT NULL,
zessionsbank varchar(100) NOT NULL,
mahnen varchar(100) NOT NULL,
verzugszinsen varchar(100) NOT NULL,
bankeinzug varchar(100) NOT NULL,
valutadatum varchar(100) NOT NULL,
opnummernkostenstelle varchar(100) NOT NULL,
skontobetrag varchar(100) NOT NULL,
fwskontobetrag varchar(100) NOT NULL,
kore varchar(1) NOT NULL default ‘0’,
PRIMARY KEY (id) );”);
mysql_close();

So, there are only 9 slices, that are relevant for the output to the DB table.

The current output looks like:

000066202;000000000;000000012;01201310;00008060 ;00000;00;0000000000010540+0;OHN/01 L

But how?

;D :smiley: :wink:

[code]<?php
$datastr=‘00000035412012013100000000000000000120120131000000000 00000000000000000000000000+0000000002012010000000000200000000000171753-00000000000000000+00000000000000000+00000000000171753-000000000000000000000000000000000000000000+00000000000000000+00000000000000000+00000000000000000+00000000000000000000000LOHN/01 LG 000000000000000000000000000000000000000000000000000+000000000000000000000000000000+000000000000000000000000 0000000000000000000000 0*’;
$keys=array(
array(0,2,9),
array(1,19,9),
array(2,28,9),
array(3,37,8),
array(4,45,9),
array(5,99,5),
array(6,104,2),
array(7,108,18),
array(8,300,18)
);
$data=array_fill(0,10,$datastr);
foreach($data as $line)
{
foreach($keys as $val)
{
$dbkey=count($db);
$db[$dbkey][$val[0]]=substr($line,$val[1],$val[2]);
}
}
header(‘Content-Type: text/plain’);
var_dump($db);

?>
[/code]

Read/Run/Read/Understand/Implement

Sponsor our Newsletter | Privacy Policy | Terms of Service