note 43467 added to function.fwrite

From: Date: Tue, 22 Jun 2004 13:20:25 +0000
Subject: note 43467 added to function.fwrite
Groups: php.notes 
Request: Send a blank email to php-notes+get-71498@lists.php.net to get a copy of this message
People seem to be having trouble removing part of a file. I have created a nifty function that will basically remove anything you want from a file. function remove_from_file($filename,$string_r,$check) { //Open the file $handle=fopen($filename,"r") or die("Error opening file"); //read it $content=fread($handle,filesize($filename)); fclose($handle); //remove what we want to remove $content=str_replace($string_r,"",$content); //delete the old file unlink($filename) or die("Error deleting old file"); //create it $handle=fopen($filename,"a") or die("Error creating file"); //write in it fwrite($handle,$content) or die("Could not write to new file"); fclose($handle); //do we want to reopen the file to check if content has been written? if($check) { $handle=fopen($filename,"r") or die("Could not reopen file for verification"); $result=fread($handle,filesize($filename)); if($result==$content) { return true; } else { return false; } } } Now everytime you want to remove something from a file, use remove_from_file("blah.txt","REMOVEME",false); if you just want to do it, but if you want to check if the modifying ID happen, use if(remove_from_file("blah.txt","REMOVEME",true)){ //blah } setting the last property to true will enable a returning of TRUE or FALSE upon if the file was modified or not. ---- Manual Page -- http://www.php.net/manual/en/function.fwrite.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+43467 Delete -- http://master.php.net/manage/user-notes.php?action=delete+43467&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+43467&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#71498) next »