note 42029 added to function.fwrite
| From: | DanielW at rn2 dot php dot net | Date: | Thu, 29 Apr 2004 23:47:39 +0000 |
| Subject: | note 42029 added to function.fwrite | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-69018@lists.php.net to get a copy of this message | ||
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
?>
----
Manual Page -- http://www.php.net/manual/en/function.fwrite.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+42029
Delete -- http://master.php.net/manage/user-notes.php?action=delete+42029&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+42029&report=yes
Search -- http://master.php.net/manage/user-notes.php