Re: ? about PHP includes

From: Date: Fri, 03 Aug 2001 04:36:38 +0000
Subject: Re: ? about PHP includes
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-61042@lists.php.net to get a copy of this message
On Fri, 3 Aug 2001 13:29, Craig Westerman wrote: > OPen the file and read in the required number of characters (short > explanation). Have a look at the file handling functions. > > -- > David Robley > > > Thanks David, > > I came up with this, but it doesn't work. Does text file need to be in > a special format? It is 4 paragraphs of plain text created in notepad. > ------------------------------------------------------------ > <html> > <head> > <title>Include Abbreviated File</title> > </head> > > <body> > > <pre> > <?php > $txtfile = fopen("text.txt", "r"); > > while (!feof($filePointer)) ?? shouldn't that be $txtfile rather than $filepointer ?? > { > $line = fgets($txtfile, 250); > print $line; > } > > close($txtfile); > > ?> > </pre> > > </body> > </html> > ------------------------------------------------------------ > > Thanks Not quite. fgets will read up to length-1 bytes _OR_ a NewLine or EOF, whichever comes first. So if your first line is < 250 chars, it'll just read that line, then go on to the next one. What you might want to do is something like $line = ''; $fp = fopen("text.txt", "r"); while (!feof($fp)) { $line .= fgets($txtfile, 250); //append to $line print $line; if(strlen($line) >= 250) { break; // jump out when enough chars received } } $line = substr($line,0,250); // bring it down to 250 chars close($fp); Or even better :-) $line = ''; $fp = fopen("text.txt", "r"); $line = fread($fp, 250); close($fp); -- David Robley Techno-JoaT, Web Maintainer, Mail List Admin, etc CENTRE FOR INJURY STUDIES Flinders University, SOUTH AUSTRALIA Why blame Clinton? He hasn't done a darn thing.

« previous php.general (#61042) next »