Re: how to delete some lines in a file
| From: | Carroll Barrett | Date: | Thu, 13 Jul 2000 20:11:36 +0000 |
| Subject: | Re: how to delete some lines in a file | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-6535@lists.php.net to get a copy of this message | ||
A little more general:
$lines=file("testfile"); // all array elements will have
trailing newline
// array manipulation here; new array elements must have
trailing newline
$fp=fopen("testfile","w");
reset($lines);
while(list($key,$val)=each($lines))
fwrite($fp,$val);
fclose($fp);
Regards,
Carroll Barrett
-----Original Message-----
From: Hans H. Anderson <me@hansanderson.com>
To: xinkun wang <xinkun_wang@hotmail.com>
Cc: php-general@lists.php.net <php-general@lists.php.net>
Date: Thursday, July 13, 2000 3:22 PM
Subject: Re: [PHP] how to delete some lines in a file
>Make sure you end the "new" with a carriage return, like
"new\n". That
>will put them all on separate lines.
>
>One way you can delete lines is this:
>
>$array = file("file.txt");
>$deleteline = 8; // what line to delete
>
>$fp = fopen("file2.txt","w");
>for($i=0;$i<count($array);$i++) {
>
> if($i!=$deleteline) fputs($fp,"$array[$i]\n");
>
>}
>fclose($fp);
>
>That code is untested, but should give you and idea on how to
proceed.
>
>Hans
>
>On Thu, 13 Jul 2000, xinkun wang wrote:
>
>> if I want to delete some lines in a file, I must fopen it and
save it in an
>> array, but I can't delete elements in the array due to lacking
of such
>> function, any ideas?
>> if I make it like for ($i=0;i<10;i++)
>> $array[$i]="new";
>> and write it to the file, the result is file wirte "new"s in
one line, not
>> in 10 line saperately. I don't know why.
>>
>>
>>
_________________________________________________________________
_______
>> Get Your Private, Free E-mail from MSN Hotmail at
http://www.hotmail.com
>>
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
>> For additional commands, e-mail:
php-general-help@lists.php.net
>> To contact the list administrators, e-mail:
php-list-admin@lists.php.net
>>
>>
>>
>
>Hans Anderson ~ me@hansanderson.com
>PO Box 426, Missoula, MT 59806 | PHP, Search Engines, SQL
> (800)397-9116 / (406)549-6524 | Databases, Flash Multimedia,
> http://www.hansanderson.com | Killer Audio Production,
> http://www.goofiness.com | Minty Fresh Breath.
>
>
>--
>PHP General Mailing List (http://www.php.net/)
>To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
>For additional commands, e-mail: php-general-help@lists.php.net
>To contact the list administrators, e-mail:
php-list-admin@lists.php.net
>