hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : PHP fopen
Reply

Programming Discussion Discussions related to web programming languages and other related issues. Topics may include configuration, optimization, practical usage and database connectivity.
Forum Jump

PHP fopen

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 08-27-2002, 02:56 AM
xerocity.com xerocity.com is offline
Junior Guru
 
Join Date: Apr 2002
Location: Sacramento, CA
Posts: 220

PHP fopen


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

Reply With Quote


Sponsored Links
  #2  
Old 08-27-2002, 03:25 AM
ranchoweb ranchoweb is offline
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/

Reply With Quote
  #3  
Old 08-27-2002, 01:33 PM
combs combs is offline
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.

Reply With Quote
Sponsored Links
  #4  
Old 08-27-2002, 07:04 PM
xerocity.com xerocity.com is offline
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

Reply With Quote
  #5  
Old 08-27-2002, 07:27 PM
ranchoweb ranchoweb is offline
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/

Reply With Quote
  #6  
Old 08-27-2002, 07:29 PM
xerocity.com xerocity.com is offline
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

Reply With Quote
  #7  
Old 08-27-2002, 07:31 PM
xerocity.com xerocity.com is offline
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

Reply With Quote
  #8  
Old 08-27-2002, 07:37 PM
ranchoweb ranchoweb is offline
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/

Reply With Quote
  #9  
Old 08-27-2002, 08:17 PM
apokalyptik apokalyptik is offline
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...

Reply With Quote
Reply

Related posts from TheWhir.com
Title Type Date Posted


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?