RE: [PHP] Inserting Lines in Files WAS [Re: PHP]
| From: | Jay Blanchard | Date: | Fri, 13 Sep 2002 12:14:16 +0000 |
| Subject: | RE: [PHP] Inserting Lines in Files WAS [Re: PHP] | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-116261@lists.php.net to get a copy of this message | ||
[snip]
> Also does anyone know how to insert a text on about the 4 or 5 line of a
file using php? I can add text to the end of a file and to the beginning,
but I'm not sure how to do it for specific lines.
>
[/snip]
Ok, I whipped of a quick set of code that I have not tested
<?php
$lineCounter = 0;
if($theFile = fopen("yourfile.txt", "r+"){
while(!feof($theFile)){
$theLine = fgets($theFile);
$lineCounter++
if($lineCounter == 4){
fputs($theFile, "the stuff you wish to add");
}
}
fclose($theFile);
}
?>
We used to do this thing with PERL many years ago for a very basic web
article publishing script where we opened a template file and searched for
comment lines and inserted the appropriate article information, which could
be done as above. As I recall it took some fairly complex regex and many
more lines of code, though I am sure it could be rewritten now.
HTH! Peace ...
Jay
Growing old is mandatory, growing up is optional
*****************************************************
* Texas PHP Developers Conf Spring 2003 *
* T Bar M Resort & Conference Center *
* New Braunfels, Texas *
* Contact jay.blanchard@niicommunications.com *
* *
* Want to present a paper or workshop? Contact now! *
*****************************************************