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.
>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
>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
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
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
>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;
I think you meant:
$size_of_array = count($lines);
But, agreed that a foreach loop is a more elegant solution.
Yeas, you are right, missed the assignment and the count.
--
//Aho
cheers,
Samuel
__________ Informatie van ESET NOD32 Antivirus, versie van database viruskenmerken 5282 (20100715) __________