
08-27-2002, 02:56 AM
|
|
Junior Guru
|
|
Join Date: Apr 2002
Location: Sacramento, CA
Posts: 220
|
|
Hello,
I would like to know if anyone out there can give me an example of code for what I need. I have tried all day today and it still will not work correctly.
What I need is some php code that will open a textfile and scan the entire textfile line by line (it is *not* a delimited file but each record is on its own line) and if it matches a variable then do nothing. But if there are no lines that match, append the variable to the end of the file.
It should be pretty simple but for some reason it isn't working right. I know that I can do this in perl (for example) but it is must be done in php.
It will be placed inside of a while statement which will change the variable and use the function multiple times.
Thank you in advance.
__________________
Joel Strellner
|

08-27-2002, 03:25 AM
|
|
Junior Guru Wannabe
|
|
Join Date: Mar 2002
Location: San Jose, CA
Posts: 34
|
|
Hi, you don't need fopen for this. the "file" function will put the whole file into an array.
$stuff = file("/path/to/textfile.txt");
for ($i=0;$i<count($stuff);$i++) {
//this will loop through each line...
//do your comparisons here
}
__________________
ranchoweb.com
reliable linux hosting with all the trimmings
http://www.ranchoweb.com/webhost/
|

08-27-2002, 01:33 PM
|
|
Junior Guru
|
|
Join Date: Aug 2002
Location: Plymouth
Posts: 212
|
|
Right. Put the whole file in array and loop through the array. Set a boolean flag to false initially. If the line matches, make it true in loop. If after the loop , flag is false that means no match were found and hence append the line in the file.
|

08-27-2002, 07:04 PM
|
|
Junior Guru
|
|
Join Date: Apr 2002
Location: Sacramento, CA
Posts: 220
|
|
Quote:
Originally posted by ranchoweb
Hi, you don't need fopen for this. the "file" function will put the whole file into an array.
$stuff = file("/path/to/textfile.txt");
for ($i=0;$i<count($stuff);$i++) {
//this will loop through each line...
//do your comparisons here
}
|
This is basically one of the pieces of code that I tried. However, what will put the current line into a variable so I can compare it? I may be incorrect but $i will only hold the line number.
Thank you again.
__________________
Joel Strellner
|

08-27-2002, 07:27 PM
|
|
Junior Guru Wannabe
|
|
Join Date: Mar 2002
Location: San Jose, CA
Posts: 34
|
|
Oh sorry, to access the current line, just use $stuff[$i]
Like so:
$stuff = file("/path/to/textfile.txt");
for ($i=0;$i<count($stuff);$i++) {
print $stuff[$i];
}
__________________
ranchoweb.com
reliable linux hosting with all the trimmings
http://www.ranchoweb.com/webhost/
|

08-27-2002, 07:29 PM
|
|
Junior Guru
|
|
Join Date: Apr 2002
Location: Sacramento, CA
Posts: 220
|
|
Quote:
Originally posted by xerocity.com
This is basically one of the pieces of code that I tried. However, what will put the current line into a variable so I can compare it? I may be incorrect but $i will only hold the line number.
Thank you again.
|
Never mind I figured it out.
Thanks.
__________________
Joel Strellner
|

08-27-2002, 07:31 PM
|
|
Junior Guru
|
|
Join Date: Apr 2002
Location: Sacramento, CA
Posts: 220
|
|
ranchoweb,
I guess I figured it out at the same time you posted the answer.
Thanks again,
__________________
Joel Strellner
|

08-27-2002, 07:37 PM
|
|
Junior Guru Wannabe
|
|
Join Date: Mar 2002
Location: San Jose, CA
Posts: 34
|
|
__________________
ranchoweb.com
reliable linux hosting with all the trimmings
http://www.ranchoweb.com/webhost/
|

08-27-2002, 08:17 PM
|
|
Newbie
|
|
Join Date: Aug 2002
Posts: 19
|
|
Different, More Clear Example
Code:
<?php
$datafile='/path/to/file';
/* Read each line that the file contains into an array - $data */
$data=file($datafile);
/* Loop through data */
foreach ( $data as $idx => $val ) {
echo 'Line #';
echo $idx;
echo ' Says: "';
echo $val;
echo '"<br>';
}
/* pull out a specific entry */
echo 'Line #2 Says: "';
// remember that arrays start at element # 0
echo $data[1];
echo '"<br>';
?>
Please note that i wrote the above DIRECTLY into the reply, i did _NOT_ debug this at all... its just an example... I find foreach loops easier to deal with...
|
Related posts from TheWhir.com
|
| Title |
Type |
Date Posted |
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
| Postbit Selector |
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
| Login: |
|
|
| Advertisement: |
|
|
| Web Hosting News: |
|
|
|