Jump to content

Reading from a text file?


exze

Recommended Posts

It's possible, of course :) The algorithm is depends on if you have just one string or more strings. Better say, if this string is the last in the file or not the last.

The reason is that if this info is the last in the file, you may add new info very easy. If it's not the last info you have to rewrite your file.

Link to comment
Share on other sites

I need it to run through the whole file adding up every 4th section.

Well... I explain an algorithm as I see it.

You need functions fopen(), fgets() (fread), fwrite() and some others. Read your file line by line, add any info and write it in another file. Then remove old file and finally give your new file the name of an old file.

In the result your will have that you need.

Link to comment
Share on other sites

Rough idea...

<?PHP
/* set the file name here */
$file = "mydata.txt";

/* put the file into an array */
$lines = file($file);

/* count the number of elements in the array */
/* each line from the file is an element of this first array */
$count = count($lines);

/* loop thru each line/element of the first array */
for($i=1;$i<$count;$i++) {
/* create a temp array of the current line */
/* presumes spaces are being used as delimiters */
$temp = explode(" ", $lines[$i]);

/* start adding the desired element of the temp array to your total */
/* note that $x is the number of the element you want added; elements start at 0 (zero) */
$total = $total + $temp[$x];
}
echo $total;
?>

Link to comment
Share on other sites

Ok thanks for far this is the format

 

'date' 'name' 'moreinformation' '1.00' 'ip'
'date' 'name' 'moreinformation' '1.00' 'ip'
'date' 'name' 'moreinformation' '1.00' 'ip'
'date' 'name' 'moreinformation' '1.00' 'ip'

 

first row is the 4th column but then it's an extra 5 columns to the next one :s

Link to comment
Share on other sites

'January 16, 2012, 7:48 pm' 'Matt' 'fatteM@live.co.uk' '1.00' '66.211.170.66'

'January 16, 2012, 7:50 pm' 'Josh' 'bchee@live.net' '0.99' '66.211.170.66'

'January 16, 2012, 8:31 pm' 'Michael' 'alidaz4@hotmail.com' '1.01' '66.211.170.66'

'January 19, 2012, 7:48 pm' 'I Needa Doc' 'joshypal@hotmail.com' '1.00' '66.211.170.66'

 

 

Link to comment
Share on other sites

Perhaps not the most elegant or efficient way, but it works. Simply replace ezra1.txt with your actual file name.

<?PHP
/* set the file name here */
$file = "ezra1.txt";
/* put the file into an array */
$lines = file($file);

/* count the number of elements in the array. Each line from the file is an element of this first array */
$count = count($lines);

/* loop thru each line/element of the first array */
for($i=1;$i<$count;$i++) {
/* create a temp array of the current line.  Presumes spaces are being used as delimiters */
$temp = explode("'", $lines[$i]);
/* start adding the desired element of the temp array to your total */
/* note that $x is the number of the element you want added; elements start at 0 (zero) */
$total = $total + $temp[7];
}
echo $total;
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.