File Manipulation Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mudvein
Forum Commoner
Posts: 45
Joined: Wed Mar 16, 2005 4:39 pm

File Manipulation Help

Post 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.
User avatar
wwwapu
Forum Contributor
Posts: 197
Joined: Wed Apr 07, 2004 11:57 am
Location: Turku, Finland

Post 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.
mudvein
Forum Commoner
Posts: 45
Joined: Wed Mar 16, 2005 4:39 pm

Post 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);
Post Reply