note 42029 deleted from function.fwrite by tomsommer
| From: | tomsommer@php.net | Date: | Fri, 30 Apr 2004 00:20:55 +0000 |
| Subject: | note 42029 deleted from function.fwrite by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-69020@lists.php.net to get a copy of this message | ||
Note Submitter: Daniel W
----
here is a function that writes to a spacific line of the file. This function is probably slow
because it has to open the file into an array and then edit the line and then write the new file.
here is the function's code:
<?
function fwritel($src, $string, $line){
$line = $line - 1;
$array = file("$src");
$hfile = fopen("$src", "w+");
$size = count($array);
if($line <= $size){
$old = $array[$line];
if($old == ""){$old = "$*$*$*$*$**#";}// make a wierd string for it to replace
$new = $string;
$new = str_replace($old, $new, $old);
$array[$line] = $new;
$new = "";
$old = "";
for($i=0; $i<$size; $i++) {
$new .= $array[$i];
}
fwrite($hfile, $new);
fclose($hfile);
}
}
?>
how to use:
<?
fwritel("filename.ext","this is the string to add", 3);
// 3 is the line number if the line does not exist it wont do anything
?>