NameSilo

Shortening (HTML/PHP)

Spaceship Spaceship
Watch

SiKing

Registered MemberEstablished Member
Impact
6
Hey all. I'm sure this is quite simple but basically I'm displaying a text file in a textarea but only want to display a certain number of lines. I'm using 'fread' but I think you can only set the number of bytes rather than number of lines. Anyway, there are three ways I'm looking at doing it:

1. With the fread function, somehow.
2. When the contents of the text file are stored as a variable.
3. ..or with the 'textarea' field itself.

Just need a little bit of guidance.

Thanks :)
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
.US domains.US domains
Something like this:
PHP:
<?php

$lines = preg_split("#\n#", file_get_contents('yourfile.txt'), -1, PREG_SPLIT_NO_EMPTY);
$data = '';
$limit = 5;

for ($i = 0; $i <= $limit; $i++)
{
	$data .= trim($lines[$i]) . "\n";
}

unset($lines);

?>
 
0
•••
Thanks for the help. Small donation sent :)
 
0
•••
Small update for SV's code.

You can replace:
PHP:
$lines = preg_split("#\n#", file_get_contents('yourfile.txt'), -1, PREG_SPLIT_NO_EMPTY);
With:
PHP:
$lines = file('yourfile.txt');

I would assume my way is faster as it only uses 1 function.
 
0
•••
http://php.net/manual/en/function.fgets.php:
Reading a file line by line
PHP:
<?php
$handle = @fopen("/tmp/inputfile.txt", "r");
if ($handle) {
    while (!feof($handle)) {
        $buffer = fgets($handle, 4096);
        echo $buffer;
    }
    fclose($handle);
}
?>
 
0
•••
Dynadot — .com Registration $8.99Dynadot — .com Registration $8.99
Appraise.net

We're social

Unstoppable Domains
Domain Recover
DomainEasy — Live Options
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back