Jump to content


Photo

php remove line from file


  • Please log in to reply
4 replies to this topic

#1 derek.sullivan

derek.sullivan

    Jedi In Training

  • Members
  • PipPip
  • 343 posts
  • Gender:Male
  • Location:Georgia
  • Interests:preaching, programming, music, friends, outdoors, moves, books

Posted 08 March 2011 - 12:11 PM

I receive no errors when running the code, but it just wont remove the line I want removed! Here is the code below:
<?php

if ($_GET['mode'] == 'del') {

$array = file('file.html');
unset($array[array_search($_GET['who'], $array)]);
echo "successful! <a href=run.php>Return</a>":

}else{

$cnt = file('file.html');

foreach ($cnt as $un) {

echo $un . "<a href=run.php?mode=del&who=$un>Remove</a>";

}

}

?>

any suggestions?

#2 derek.sullivan

derek.sullivan

    Jedi In Training

  • Members
  • PipPip
  • 343 posts
  • Gender:Male
  • Location:Georgia
  • Interests:preaching, programming, music, friends, outdoors, moves, books

Posted 08 March 2011 - 12:50 PM

Not sure what you mean

#3 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 08 March 2011 - 01:49 PM

Like working in notepad, if you open a file and delete a line, it doesn't automatically save it. You have to save it yourself. Which means you need to use a function like fwrite() to save the data.

#4 derek.sullivan

derek.sullivan

    Jedi In Training

  • Members
  • PipPip
  • 343 posts
  • Gender:Male
  • Location:Georgia
  • Interests:preaching, programming, music, friends, outdoors, moves, books

Posted 08 March 2011 - 01:59 PM

Okay, so would I do something like:

$file = file('file.html');
$new = unset($file[search_array(trim($_GET['who']), $file)]));
$fp = fopen($file, 'r+');
fwrite($fp, $new);
fclose($fp);

Edited by derek.sullivan, 08 March 2011 - 02:06 PM.


#5 derek.sullivan

derek.sullivan

    Jedi In Training

  • Members
  • PipPip
  • 343 posts
  • Gender:Male
  • Location:Georgia
  • Interests:preaching, programming, music, friends, outdoors, moves, books

Posted 08 March 2011 - 02:13 PM

Adam that makes so much sense! Unfortunately, I was unwise when I learned how to code. I did not open myself up to all of PHP. I limited my knowledge, so now what I am trying to do is learn every aspect. This does help, and I will try this.

It kinda did what it was suppose to do. However, it would add a newline each time with the $_GET['who'] and it would continue to add on to that name ex: "\n\n\nBobBobBobBobBob" just an example and yes it did add the \n's as well

here is the script I am running so you can see what is happening : http://dsullweb.net/chat/run.php

solved

Edited by rc69, 08 March 2011 - 10:29 PM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users