Page 1 of 1
File Manipulation Help
Posted: Wed Jun 29, 2005 9:43 am
by mudvein
If I want to remove just the first line and the last line in a file, how would i do that? I also want to shift all the file contents up a line and remove the last blank line ( or would it automatically do that?) If anyone can help at all, I'd be most greatful. Thanks.
Posted: Wed Jun 29, 2005 11:18 am
by wwwapu
You can do something like this if you like
Code: Select all
<?php
//"remove" the first and last
$file="test.txt";
$lines=file($file);
$new_data="";
for($i=1; $i<count($lines)-1; $i++){
$new_data.= $lines[$i];
}
//Trim the empty line
$new_data=trim($new_data);
$open=fopen($file, "w");
fwrite($open, $new_data);
fclose($open);
?>
This does not actually remove anything from anywhere. It just does not write it back there.
Posted: Wed Jun 29, 2005 11:41 pm
by mudvein
well... that's not exactly what i was needing. however, i figured out how to do it by :
Code: Select all
str_replace('this_string','',$fileContent);