Dynadot Aftermarket: discover domain gems worth investing in. Visit our aftermarket.

Shortening (HTML/PHP)

SpaceshipSpaceship
SpaceshipSpaceship
Namecheap AuctionsNamecheap Auctions
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
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
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
Close
Thanks for the help. Small donation sent :)
 
0
Close
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
Close
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
Close
DomainEasy: white label portfolios are live. Enable yours.

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Filters for activity feeds
Your news feed, Latest activity, and the activity tab on profiles now have filter buttons: All, Posts, Quick replies, and Other. Your...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back