Jump to content

Reading and writing to specific parts of .txt


mathiasppc

Recommended Posts

Is this possible? Could the following code be replaced with one .txt consisting of two digits?

 

$myFile = "offerrotation.txt";
$myFile2 = "lprotation.txt";

$fh = @fopen($myFile, 'r');
$offerNumber = @fread($fh, 5);
@fclose($fh);

$fh = @fopen($myFile2, 'r');
$lpNumber = @fread($fh, 5);
@fclose($fh);

 

Many thanks

Link to comment
Share on other sites

You'd be better having the digits one per line, then using file to read the contents of the file into an array:

 

$rotation = file('rotation.txt');

$rotation[0] // contains first number
$rotation[1] // contains second number

Link to comment
Share on other sites

If you write them both at the same time, you can just concatenate them within a string and use the new-line character ("\n") to put them on different lines:

 

$new_rotation = $new_offer . "\n" . $new_lp;
file_put_contents('rotation.txt', $new_rotation);

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.