Forum Moderators: coopster
I really start to LOVE PHP :) , almost feels like Basic (almost)
I've read plenty of code, to read a line from a txt file, as a whole line, also code on howto read only the first X contents.
i preffer a text file with contents like these;
john ¦ 47 ¦ 2 ¦ 63
mary ¦ 32 ¦ 90 ¦ 23
dick ¦ 2 ¦ 22 ¦ 101
and to want to be able to process john into var1, "47" into var2, "2" into var3 and "63" into var4
do my calculations, change for example var4 (in the text file), and proceed to do the same with mary, then dick, and then the next one...
(in short: the problem is reading and changing the independent vars)
this must be possible?
Since text files do not have fixed record sizes partial writes are *extremely* hard to do and will make your program much more complicated.
I'd just use fgetcsv() and put each returned array into an array (ie a multidimensional array). Then change what you will. And then have something like:
foreach( $bigarray as $line ) {
$outstr="${line[0]}¦${line[1]}¦${line[2]}¦${line[3]}\n";
fwrite($fp,$outstr,strlen($outstr));
}
daisho.
(¦) symbol for a delimiter. I prefer tab-delimited. It may seem trivial now, but if you ever need to process HTML form data, you'll see why. People can key a pipe symbol, if they try to key a tab, it moves their cursor insertion point to the next input-capable object. OK, you might be thinking, I'll just use a text qualifier (the double-quotes in this text file snippet are text-qualifiers):
"john"¦"47"¦"2"¦"63"
"mary"¦"32"¦"90"¦"23"
"dick"¦"2"¦"22"¦"101"
If magic_quotes_runtime is enabled, most functions that return data from any sort of external source including databases and text files will have quotes escaped with a backslash.
As for what I have strlen. Looking at the definition of fwrite [php.net] it shows a 3rd optional paramater for length. You can leave it off. I just like being specific in my code. I do not like to "assume" things even if it's in the spec :)
daisho.