Archived

This topic is now archived and is closed to further replies.

try_catch_this

any php programmers out there?

Recommended Posts

try_catch_this    373
<?php
$game    = $_GET['game'];
$version = $_GET['version'];
$nick    = $_GET['nick'];
$IP      = getenv('REMOTE_ADDR');
$port    = $_GET['port'];

$proxy = $HTTP_SERVER_VARS["REMOTE_ADDR"];
$host  = @gethostbyaddr( $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"] );

$GameAttributes =       "game="    . $game    .
                  "+" . "version=" . $version .
                  "+" . "nick="    . $nick    .
                  "+" . "IP="      . $IP      .
                  "+" . "port="    . $port;

$ltime = localtime( time(0), 1 );
         // year ,           day of the year     hour                min                second

$time  = $ltime['tm_year'] . $ltime['tm_yday'] . $ltime['tm_hour'] . $ltime['tm_min'] . $ltime['tm_sec'];


$log = fopen( "accesslog.txt", "a+" );
if( $log )
{
   fwrite( $log, "erasegame+" . $GameAttributes . "+" . "time=" . $time . "\r\n" );
   fclose( $log );
}

$games = file( "games.txt" );

foreach( $games as $game )
{
   rtrim( $game );
   echo( $game );
}

$key   = array_search( $GameAttributes, $games );
if( $key )
{
   $games = array_splice( $games, $key );
   if( is_writable("games.txt") )
   {
      $handle = fopen( "games.txt", "w" );
      foreach( $games as $game_num => $game )
      {
         fwrite( $handle, $game );
      }
      fclose( $handle );
      echo( "game erased" );
   }
   else
   {
      echo( "game could not be erased, file not writeable" );
   }
}
else
{
   echo( "game not found" );
}
?>

I want to be able to erase a single line from a text file in php. I am kind of new to the language and what I am doing dosent seem to be working. For one thing the rtrim() only removes the \r\n from the first line in the file. Anyone know what I am doing wrong here?
oglpong Online (47% complete)
gdipong
If you got any requests or suggestions post here [edited by - try_catch_this on May 25, 2004 2:59:32 AM]

Share this post


Link to post
Share on other sites
Colin Jeanne    1114
If you want to remove an arbitrary line from a file you have to read in the entire file and then write back everything except that one line.


Thanks Salsa!Colin Jeanne | Invader''s Realm
"I forgot I had the Scroll Lock key until a few weeks ago when some asshole program used it. It even used it right" - Conner McCloud

Share this post


Link to post
Share on other sites
try_catch_this    373
quote:
Original post by Invader X
If you want to remove an arbitrary line from a file you have to read in the entire file and then write back everything except that one line.


Thanks Salsa!Colin Jeanne | Invader''s Realm
"I forgot I had the Scroll Lock key until a few weeks ago when some asshole program used it. It even used it right" - Conner McCloud


Hmmm,
From the php docs:

(PHP 3, PHP 4 )
file -- Reads entire file into an array

I was asking for someone who knew php. This is not a problem where I dont know what to do it one where I dont know how to implement it.

Share this post


Link to post
Share on other sites
l3mon    128
Well first you need an array. Then you read in the file, flush it, and reinsert everything except the first line.
Of course you want to implement some checks... if you are unsure ask again, but check the php documentation, it''s pretty straight forward...


$myArray = file("myTextFile.txt");
$handle = fopen("myTextFile.txt", ''w'');

for($i=1; $i < count($myArray); $i++)
fwrite($handle, $myArray[$i]);


I think this should work

Share this post


Link to post
Share on other sites
Malone1234    139
foreach( $games as $game )
{
rtrim( $game );
echo( $game );
}

$game is essentially a local variable within the scope of that loop. For each index in the $games array, the line of text is copied into the $game variable. Therefore, when you use the $games array later to write back into the file, all the ''\r\n'' characters are still there. You need to copy the chopped lines into a new array and write that out to a file, or else do it all at once as the previous poster demonstrated.

Share this post


Link to post
Share on other sites