1,105,229 Community Members

Write to line in file.

Member Avatar
websponge
Junior Poster in Training
59 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi, I have been able to write to a text file, then another page can read from that file, all good.. Problem is I'm using multiple txt files just to set various variables. There is a reason I'm not using a database.

Can I somehow, write to a file after a certain marker is found ?

I.e, if I had in my txt file:

variable1

variable2

I could write to the text file a value, after it saw #variable2
So it would then read

variable2mypostedvalue ?

And of course do the same in reverse when reading the file?

Thanks

Member Avatar
minitauros
Practically a Master Poster
606 posts since Apr 2011
Reputation Points: 67 [?]
Q&As Helped to Solve: 104 [?]
Skill Endorsements: 6 [?]
 
0
 

Hm yes I guess you could. You can place the internal pointer of a file to a specific position using fseek(). However, I've never used it before myself, so I wouldn't know exactly how to use it.

What you COULD do is, if you know the exact value of the variable you are seeking, use a strpos() or stripos() to find the offset of the word you are looking for, then split the string there and insert whatever you want to insert. For example:

<?php
// Imaginary file contents.
$file_contents = 'This is a file. It contains text.';

// We're looking for this word:
$looking_for = 'file';

// We want to add this word after the word we're looking for:
$add = '. It is HUGE.';

// Find its offset.
$offset = stripos($file_contents, $looking_for);

// Split the string.
$substr_to = $offset + strlen($looking_for);
$part_1 = substr($file_contents, 0, $substr_to);
$part_2 = substr($file_contents, $substr_to);

$new_file_contents = $part_1 . $add . $part_2;

// $new_file_contents is now:
// 'This is a file. It is HUGE. It contains text.';
Member Avatar
JorgeM
IT Addict
6,381 posts since Dec 2011
Reputation Points: 567 [?]
Q&As Helped to Solve: 953 [?]
Skill Endorsements: 172 [?]
Moderator
Featured
Sponsor
 
0
 

Is the text file a requirement? Have you considered using an XML file which could provide you with better/easier structure to handle?

also, you mention storing variables..is this because you need to keep some variable for long term storage, more than just a user's session? Or are you trying to store some type of global variable?

Member Avatar
websponge
Junior Poster in Training
59 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

I have a large wall monitor at work, that needs to display where an engineer is on a certain job, so i have WAMP running on my desktop PC and the wall screen points to the results.php page, on my desktop I can write the variables to a text file, so that when the wall screen is refreshed it reads the results from the text file.

there are a few different things the screen displays, so wanted to have 1 text file that can be overwritten on certain lines..

Member Avatar
minitauros
Practically a Master Poster
606 posts since Apr 2011
Reputation Points: 67 [?]
Q&As Helped to Solve: 104 [?]
Skill Endorsements: 6 [?]
 
0
 

What about using a .csv file? These files can be easily edited using Microsoft Excel and can be easily read and traversed by PHP.

Member Avatar
websponge
Junior Poster in Training
59 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

I wanted to do that originally, but wasnt sure how you go about writing to certain places in a CSV file.. if its easier Ill do that, could you advise how I can write to a certain place in a CSV then?

Member Avatar
minitauros
Practically a Master Poster
606 posts since Apr 2011
Reputation Points: 67 [?]
Q&As Helped to Solve: 104 [?]
Skill Endorsements: 6 [?]
 
0
 

Well for as far as I know there exist PHP functions for reading a CSV file to an array. You can then modify that array, add whatever you want, and then overwrite the CSV file with the new info. That is how I think it should work. Check out fgetcsv().

Also, if the file is not too big, you could also just serialize an array and write that to a file. That way you can work with an array even more easily.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article