Forum Moderators: coopster

Message Too Old, No Replies

reading from .txt and changing specified var's in .txt

php txt reading changing variables

         

NL_Cartman

10:54 am on Sep 25, 2003 (gmt 0)

10+ Year Member



Hi,

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?

mogwai

12:37 pm on Sep 25, 2003 (gmt 0)

10+ Year Member



Use the file() function to read each line into an array. Then use the explode() function to break the line into an array.

$values = explode("¦", $line);

$values[0] = "John"
$values[1] = "47"
$values[2] = "2"
$values[3] = "63"

Hope this helps

daisho

1:21 pm on Sep 25, 2003 (gmt 0)

10+ Year Member



Take a look at fgetcsv() [php.net]. This will do the read and explode steps in one.

daisho.

NL_Cartman

2:21 pm on Sep 25, 2003 (gmt 0)

10+ Year Member



thanx, both will work fine :)

but, ime still looking for a "method" to change the data for example var2, in the data.txt?

daisho

2:46 pm on Sep 25, 2003 (gmt 0)

10+ Year Member



With text files the only way to do this is the rewrite the entire text file with the changed data.

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.

coopster

3:07 pm on Sep 25, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I notice you prefer using the pipe
(¦)
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"

Well, any character you decide to use for a text-qualifier, they can type in as well. It's just my own humble opinion, but you may want to consider using tab-delimited.

NL_Cartman

3:25 pm on Sep 25, 2003 (gmt 0)

10+ Year Member



thanx again daisho,

I understand most of the code you presented, i'me just wondering why "strlen" is needed in this function?

also thanx coopster, since my data is not provided by the user, this wont be a problem for me.

--> but.. how does one specify a Tab as delimiter?

coopster

3:33 pm on Sep 25, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



If the fwrite() length argument is given, then the magic_quotes_runtime [us4.php.net] configuration option will be ignored and no slashes will be stripped from string.

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.

daisho

3:33 pm on Sep 25, 2003 (gmt 0)

10+ Year Member



to specify tab as delimeter simply do "\t". (The double quotes are important, single quotes do not parse for escape characters)

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.

coopster

3:34 pm on Sep 25, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Tab as delimiter is \t
<edit>Thanks daisho<edit>

NL_Cartman

11:52 am on Sep 26, 2003 (gmt 0)

10+ Year Member



Hi , thanx for all your help guyZ