Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Remove lines from textfile by length

0 views
Skip to first unread message

Samuel van Laere

unread,
Jul 10, 2010, 1:32:25 PM7/10/10
to
Currenly I need some functionality that permits me to delete lines by a
certain length from a textfile.
Bascily all lines that have 17 chars or less should be deleted.

Any hints on how to work this out?

cheers,
Samuel van Laere

__________ Informatie van ESET NOD32 Antivirus, versie van database viruskenmerken 5267 (20100710) __________

Het bericht is gecontroleerd door ESET NOD32 Antivirus.

http://www.eset.com


Geoff Berrow

unread,
Jul 10, 2010, 1:45:20 PM7/10/10
to
On Sat, 10 Jul 2010 19:32:25 +0200, "Samuel van Laere"
<webmast...@gmeel.com> wrote:

>Currenly I need some functionality that permits me to delete lines by a
>certain length from a textfile.
>Bascily all lines that have 17 chars or less should be deleted.
>
>Any hints on how to work this out?

The function file() puts the contents into an array. Loop through the
array adding lines >1 chars in length to a variable. Use fopen() and
fwrite() to save the file.
--
Geoff Berrow (Put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs www.4theweb.co.uk/rfdmaker

Geoff Berrow

unread,
Jul 10, 2010, 1:46:47 PM7/10/10
to
On Sat, 10 Jul 2010 18:45:20 +0100, Geoff Berrow
<blth...@ckdog.co.uk> wrote:

>On Sat, 10 Jul 2010 19:32:25 +0200, "Samuel van Laere"
><webmast...@gmeel.com> wrote:
>
>>Currenly I need some functionality that permits me to delete lines by a
>>certain length from a textfile.
>>Bascily all lines that have 17 chars or less should be deleted.
>>
>>Any hints on how to work this out?
>
>The function file() puts the contents into an array. Loop through the
>array adding lines >1 chars in length to a variable. Use fopen() and
>fwrite() to save the file.

Sorry, that should have been >17 chars

Samuel van Laere

unread,
Jul 10, 2010, 2:03:37 PM7/10/10
to
"Geoff Berrow" <blth...@ckdog.co.uk> schreef in bericht
news:nfch36l5ob102h8he...@4ax.com...

> On Sat, 10 Jul 2010 18:45:20 +0100, Geoff Berrow
>
>>The function file() puts the contents into an array. Loop through the
>>array adding lines >1 chars in length to a variable. Use fopen() and
>>fwrite() to save the file.
>
> Sorry, that should have been >17 chars


This is what I got, but where do I put the >17 part??

<?php
$lines = file('text.txt');

echo '<ol>' . PHP_EOL;
for($i = 0; $i < count($lines); $i++) {
$value = $lines[$i];
echo '<li>'.$value.'</li>' . PHP_EOL;
}
echo '</ol>' . PHP_EOL;
?>

Thanks allready, you've put me in the right direction.
But after all those years array's are still the hardest part to gasp :)

cheers,
Samuel

J.O. Aho

unread,
Jul 10, 2010, 3:29:06 PM7/10/10
to
Samuel van Laere wrote:
> "Geoff Berrow" <blth...@ckdog.co.uk> schreef in bericht
> news:nfch36l5ob102h8he...@4ax.com...
>> On Sat, 10 Jul 2010 18:45:20 +0100, Geoff Berrow
>>
>>> The function file() puts the contents into an array. Loop through the
>>> array adding lines >1 chars in length to a variable. Use fopen() and
>>> fwrite() to save the file.
>>
>> Sorry, that should have been >17 chars
>
>
> This is what I got, but where do I put the >17 part??

Inside the loop

> <?php
> $lines = file('text.txt');
>
> echo '<ol>' . PHP_EOL;
> for($i = 0; $i < count($lines); $i++) {

Don't use count() in for, as you do a calculation of the size of the array
each time, instead use

$size_of_array($lines);
for($i = 0; $i < $size_of_array; $i++) {
...
}

or even better IMHO

foreach($lines as $key => $value) {
if(strlen($value) <= 17)
unset($lines[$key]);
}

The array $lines will now just contain those rows with more than 17 characters
(depending on your environment settings and the text, some characters may take
up more than one "character" length, you may need to use mb_strlen()).

> $value = $lines[$i];
> echo '<li>'.$value.'</li>' . PHP_EOL;
> }
> echo '</ol>' . PHP_EOL;
> ?>

I do recommend you don't echo out everything as soon as possible, it's better
to wait until you know there is something to display, say the array would be
empty, then there is no point in echo out "<ol></ol>", it's better then to
display a message that the file was empty.


> Thanks allready, you've put me in the right direction.
> But after all those years array's are still the hardest part to gasp :)

Arrays are quite simple, a single dimension array is like a string and of
course as in a good programming language so the first cell is number 0, so the
first character in the string is 0, and the next one is 1 and so on.

--

//Aho

Geoff Berrow

unread,
Jul 10, 2010, 3:37:24 PM7/10/10
to
On Sat, 10 Jul 2010 20:03:37 +0200, "Samuel van Laere"
<webmast...@gmeel.com> wrote:

>This is what I got, but where do I put the >17 part??
>
><?php

> = file('text.txt');
>
>echo '<ol>' . PHP_EOL;
>for($i = 0; $i < count($lines); $i++) {
> $value = $lines[$i];
> echo '<li>'.$value.'</li>' . PHP_EOL;
>}
>echo '</ol>' . PHP_EOL;
>?>

Does this help?

$lines = file('text.txt');

$newcontent="";
foreach($lines as $val){
if (strlen($val)>17){
$newcontent.=$val."\n";
}
}

echo $newcontent;

matt

unread,
Jul 12, 2010, 10:20:32 AM7/12/10
to
On Jul 10, 3:29 pm, "J.O. Aho" <u...@example.net> wrote:

> Samuel van Laere wrote:
>
> $size_of_array($lines);
> for($i = 0; $i < $size_of_array; $i++) {
>  ...
>
> }

I think you meant:

$size_of_array = count($lines);

But, agreed that a foreach loop is a more elegant solution.

J.O. Aho

unread,
Jul 12, 2010, 12:12:26 PM7/12/10
to

Yeas, you are right, missed the assignment and the count.

--

//Aho

Samuel van Laere

unread,
Jul 15, 2010, 8:13:55 PM7/15/10
to
Thanks for the hulp folks,
i love it when a plan comes together.

cheers,
Samuel

__________ Informatie van ESET NOD32 Antivirus, versie van database viruskenmerken 5282 (20100715) __________

0 new messages