0

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

4
Contributors
16
Replies
18
Views
5 Years
Discussion Span
Last Post by apa3
Featured Replies
  • 1
    cereal 1,419   5 Years Ago

    You can use str_split(): [url]http://www.php.net/manual/en/function.str-split.php[/url] [code]$string = file_get_contents('file.ext'); # source file echo implode(';',str_split($string,10));[/code] Read More

  • 1

    Your code currently works for one line, but it will give undesired results when you have multiple lines in your input. I think you'll want this: [CODE] $fh = fopen('uploads/datei.txt'); while(!feof($fh)) { $string = fgets($fh); $array = array (); // clear the array // you do not NEED to specify … Read More

0

Dear valued CEREAL, first of all THANK YOU VERY MUCH for replying so fast.

Your code example made me a bit more understanding, how I could proceed. Due to my lack in FILE MANIPULATION SKILLS I have to ask a bit more specific:

The shown string hast 480 digits in total plus a "*" as terminator. Again: It looks as the following:

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

Now my nasty job is to cut that string (and the strings/lines below this one - not shown here) from left to right to the following portions (from - to):

1. 1,
2. 2 - 10,
3. 11 - 18,
4. 19 - 27,
5. 28 - 36,
6. 37 - 44,
7. 45 - 53,
8. 54,
9. 55 - 63,
10. 64 - 81,
11. 82 - 85,
12. 86 - 90,
13. 91 - 96,
14. 97 - 98,
15. 99 - 103,
16. 104 - 105,
17. 106,
18. 107,
19. 108 - 125,
20. 126 - 143,
21. 144 - 161,
22. 162 - 179,
23. 180 - 181,
24. 182 - 185,
25. 186 - 198,
26. 199 - 204,
27. 205 - 222,
28. 223 - 240,
29. 241 - 258,
30. 259 - 276,
31. 277 - 280,
32. 281 - 293,
33. 294 - 299,
34. and so on - and so on.

AND: After/while doing this, I have to write certain/specific (not all, because some of the slices are unimportant for the target file) snippets into the .CSV file.

Well, it seems to be not too complicated, but I AM TOO ... unexperienced.

Thank you in advance for taking the time.

SAM

3

cereal's code will work perfectly if you need to split it into strings of the same length. To split it into variable lengths, then you should use substr() http://php.net/manual/en/function.substr.php like this:

$string = file_get_contents('file.ext');
$array[].=substr($string,a,b); //a is where you start your string. First character in a string is at position 0. b is how many characters you need to get
$array[].=substr($string,a,b); //repeat for each slice you need to get
$new_variable = implode(';',$array); //now you'll a new variable might look like 000000;354120120131;

To write it to a csv, you can use fputcsv(); http://php.net/manual/en/function.fputcsv.php. CSV files are comma delimited, so to use fputcsv(), then you will just leave out the $new_variable = implode(';',$array); line from the previous code, and use:

$fp = fopen('file.csv', 'w'); //opens file.csv and clears its contents if it exists or creates a new one if it doesn't exist.

    fputcsv($fp, $array); //Inserts array into file.csv with each item in the array separated by a comma.

fclose($fp);

If you do need the file to be separated by ';' instead of ',', then you can use fwrite() http://php.net/manual/en/function.fwrite.php. Just add this to my first chunk of code:

$fp = fopen('file.csv','w');//could be a different file extension
$fwrite($fp,$new_variable);//will write to the file all of the chunked $array items that we separated with ';' in the implode function
fclose($fp);

Edited by grant.baker: code error

Votes + Comments
Thank you so much for your great support! Regards ... SAM.
great
1

Oh, I noticed that you said your file has multiple lines. To read the file line-by-line, use fgets() http://php.net/manual/en/function.fgets.php like this:

$fh='file.ext';
while(!feof($fh)){//this will loop through the file line-by-line until you reach the end of the file.
$string = fgets($fh);
$array[].=substr($string,a,b); //a is where you start your string. First character in a string is at position 0. b is how many characters you need to get
$array[].=substr($string,a,b); //repeat for each slice you need to get
}
$new_variable = implode(';',$array); //now you'll a new variable might look like 000000;354120120131;

Edited by grant.baker: n/a

0

Dear valued grant.baker,

thanks a lot for helping. Now the code looks like:


<?php

$fh='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 unluckily get, is the following:


Warning: fgets(): supplied argument is not a valid stream resource in /var/www/web8/html/scline_fibu_import.php on line 5

Warning: feof(): supplied argument is not a valid stream resource in /var/www/web8/html/scline_fibu_import.php on line 4

Could you please look over that again?

Thank your very much!

SAM

0

Thank you again. Sorry, but it seems to be the same problem:

Warning: feof(): supplied argument is not a valid stream resource in /var/www/web8/html/scline_fibu_import_NEW.php on line 4

Warning: fgets(): supplied argument is not a valid stream resource in /var/www/web8/html/scline_fibu_import_NEW.php on line 5

Code looks now like:

<?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;

?>

You may see it in action at:

http://datenimport.apa3.co.uk/scline_fibu_import_NEW.php

Thank you so much.

1

Ok, yea I see my other error! I forgot that you have to tell php how you want to open it. Try $fh=fopen('uploads/datei.txt','r'); That opens it with read-only privileges.

1

Your code currently works for one line, but it will give undesired results when you have multiple lines in your input. I think you'll want this:

$fh = fopen('uploads/datei.txt');
while(!feof($fh)) {
  $string = fgets($fh);
  $array = array (); // clear the array

  // you do not NEED to specify the index
  // but it is zero based, omitting 0 will add an empty ; at the start
  $array[] = substr($string,   2,  9);
  $array[] = substr($string,  19,  9);
  $array[] = substr($string,  28,  9);
  $array[] = substr($string,  37,  8);
  $array[] = substr($string,  45,  9);
  $array[] = substr($string,  99,  5);
  $array[] = substr($string, 104,  2);
  $array[] = substr($string, 108, 18);
  $array[] = substr($string, 300, 18);

  // output within the loop
  $new_variable = implode(';', $array);
  echo $new_variable . "\n";
}

Thanks to grant.baker of course, for his work ;)

Edited by pritaeas: n/a

Votes + Comments
Thank you so much! SAM.
0

Yes, thank you. But:

This code

<?php

$fh = fopen('uploads/datei.txt');
while(!feof($fh)) {
$string = fgets($fh);
$array = array (); // clear the array

// you do not NEED to specify the index
// but it is zero based
$array[] = substr($string, 2, 9);
$array[] = substr($string, 19, 9);
$array[] = substr($string, 28, 9);
$array[] = substr($string, 37, 8);
$array[] = substr($string, 45, 9);
$array[] = substr($string, 99, 5);
$array[] = substr($string, 104, 2);
$array[] = substr($string, 108, 18);
$array[] = substr($string, 300, 18);

// output within the loop
$new_variable = implode(';', $array);
echo $new_variable . "\n";
}

?>

is doing that:

Warning: fopen() expects at least 2 parameters, 1 given in /var/www/web8/html/scline_fibu_import_NEW.php on line 3

Warning: feof(): supplied argument is not a valid stream resource in /var/www/web8/html/scline_fibu_import_NEW.php on line 4

Warning: fgets(): supplied argument is not a valid stream resource in /var/www/web8/html/scline_fibu_import_NEW.php on line 5

I am sorry.

0

Yea, you need to tell php what mode to open the file with. You need fopen('uploads/datei.txt','r'); instead of fopen('uploads/datei.txt');

0

YEEEEEEESSSSS! Thank your very much.

Code looks now like that:

<?php

$fh = fopen('uploads/datei.txt','r');
while(!feof($fh)) {
$string = fgets($fh);
$array = array (); // clear the array

// you do not NEED to specify the index
// but it is zero based
$array[] = substr($string, 2, 9);
$array[] = substr($string, 19, 9);
$array[] = substr($string, 28, 9);
$array[] = substr($string, 37, 8);
$array[] = substr($string, 45, 9);
$array[] = substr($string, 99, 5);
$array[] = substr($string, 104, 2);
$array[] = substr($string, 108, 18);
$array[] = substr($string, 300, 18);

// output within the loop
$new_variable = implode(';', $array);
echo $new_variable . "\n<br>";
}

?>

Have included the "<br> tag to have it formatted on the screen.

Is there a way (of course there is) to give each slice a certain value and to write it into my MYSQL database as well?

Is there anything, I could do for you? I have a web hosting company. Just in case, that you need L/A/M/P hosting as my "thank you" for free, let me know.

Maybe I will come back with further questions. :-)

Thank you.

SAM

0

Arrays are automatically numbered starting with 0. You can also name them whatever you want, i.e. $array=substr($string, 2, 9);. Since arrays start at 0, echoing $array[0] will give you the same result as $array, so you wouldn't have to explicitly name each array item unless it will help you keep them straight for the MySQL statement. Do you need help with how to do a MySQL insert?

0

Dear grant.baker,

yes, I would like to ask for help with the MySQL inserts.


No I do have to insert the slices (values) into the MySQL table, that has the following structure:

`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',

I "only" have to write the nine (9) values (slices) from above into certain fields of the table. The rest of the table will remain empty. AFTER writing the values to the specific fields of the table I will have to export EVERYTHIN to a .CSV file.

Great to hear from you.

Thank you so much.

SAM

Arrays are automatically numbered starting with 0. You can also name them whatever you want, i.e. $array=substr($string, 2, 9);. Since arrays start at 0, echoing $array[0] will give you the same result as $array, so you wouldn't have to explicitly name each array item unless it will help you keep them straight for the MySQL statement. Do you need help with how to do a MySQL insert?

0

Ok, to make things simple, let's just say the 9 slices that you have in the array go in the first 9 columns of the table. To do that, this would be your mySQL statement:

$con = mysql_connect("localhost","your_username","your_password"); //change username and password to your username and password for your mySQL information
  if(!con)
   {
      die('Could not connect: '.mysql_error()); //outputs an error if the connection fails
   }
mysql_select_db("database_name",$con); //Selects the database

$sql = "INSERT INTO table_name (id, kennung, zusatzkennung, buchungsdatum, beleg, buchungstext, sollkonto, habenkonto, buchungsbetrag) VALUES ('$array[0]','$array[1]','$array[2]','$array[3]','$array[4]','$array[5]','$array[6]','$array[7]','$array[8]')";//Just adjust the columns and values to your needs. The first value goes in the first column, and so on(id=$array[0], habenkonto=$array[7]). 

if(!mysql_query($sql,$con))
				{
				die('Error: '.mysql_error());
			}
			else {echo "HTML Success message--you could also remove this else statement if you don't need a success statement. In that case, a message would only be displayed if an error occurred.";}

As for exporting the database to csv, a quick google search brought me to this site, which should set you on the right track. http://911-need-code-help.blogspot.com/2009/07/export-mysql-data-to-csv-using-php.html

0

Dear valued grant.baker,

thank you so very much! Everything has been working now.

WHAT MAY I DO FOR YOU to say THANK YOU?

All the best!

SAM

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.