php_joe Posted September 30, 2007 Share Posted September 30, 2007 is there a way to read a text document into an array, like with the file() function, but have it stop reading the file after just one or two lines? Example: If I wanted to read just the first line of the following text? Text: Header1, Header2, Header3 Dog, Big, Loyal Cat, Medium, Aloof Mouse, Small, Scavenger I know that fread can be stopped after a specified number by bytes has been read. is there a way to have it read until it hits a "\n"? Quote Link to comment https://forums.phpfreaks.com/topic/71269-read-only-1st-line-of-a-text-document/ Share on other sites More sharing options...
zq29 Posted September 30, 2007 Share Posted September 30, 2007 You can't do that with the file() function, neither can I think of one that you can - Although not a perfect solution, could you set the maxlen parameter of the file_get_contents() function? Or would that be too vague? Quote Link to comment https://forums.phpfreaks.com/topic/71269-read-only-1st-line-of-a-text-document/#findComment-358470 Share on other sites More sharing options...
php_joe Posted September 30, 2007 Author Share Posted September 30, 2007 You can't do that with the file() function, neither can I think of one that you can - Although not a perfect solution, could you set the maxlen parameter of the file_get_contents() function? Or would that be too vague? That's what I'll probably end up doing, I was just hoping for something a little cleaner. Quote Link to comment https://forums.phpfreaks.com/topic/71269-read-only-1st-line-of-a-text-document/#findComment-358471 Share on other sites More sharing options...
wildteen88 Posted September 30, 2007 Share Posted September 30, 2007 Can't you just use file, set the variables you want and then unset the handler? Eg: $file = file('myfile.txt'); // set the variables for the first two lines. $line1 = $file[0]; $line2 = $file[1]; // remove handler. unset($file). Quote Link to comment https://forums.phpfreaks.com/topic/71269-read-only-1st-line-of-a-text-document/#findComment-358474 Share on other sites More sharing options...
NoSalt Posted September 30, 2007 Share Posted September 30, 2007 In PHP 6.0 they should include "head" and "tail" functions like in the Un*x shell. Boy that would make things nice. Quote Link to comment https://forums.phpfreaks.com/topic/71269-read-only-1st-line-of-a-text-document/#findComment-358477 Share on other sites More sharing options...
zq29 Posted September 30, 2007 Share Posted September 30, 2007 I originally though that, but I think the OP is trying to pull the top line out of a huge file and trying to do it a bit quicker... Wouldn't that load the whole file into an array before unsetting the handler? Only guessing. Quote Link to comment https://forums.phpfreaks.com/topic/71269-read-only-1st-line-of-a-text-document/#findComment-358479 Share on other sites More sharing options...
php_joe Posted September 30, 2007 Author Share Posted September 30, 2007 Exactly! I've got a rather large file full of information. The first page that you see has a search form on it that allows the viewer to search for information within each column, and it uses the first line of the document for the search topics. At the moment I'm using file() and then just $line[0] to get the info but I'd like to have the page load faster if it's possible so I was looking for a way to avoid loading the entire document when it's not needed. Quote Link to comment https://forums.phpfreaks.com/topic/71269-read-only-1st-line-of-a-text-document/#findComment-358501 Share on other sites More sharing options...
Cagecrawler Posted September 30, 2007 Share Posted September 30, 2007 Can't you use fgets()? http://www.php.net/manual/en/function.fgets.php <?php $handle = fopen("file.txt", "r"); $firstline = fgets($handle); echo $firstline; ?> Quote Link to comment https://forums.phpfreaks.com/topic/71269-read-only-1st-line-of-a-text-document/#findComment-358503 Share on other sites More sharing options...
sKunKbad Posted September 30, 2007 Share Posted September 30, 2007 <?php $lines = file('http://www.yoursite.com'); foreach ($lines as $line_num => $line) { echo str_replace(""," ",htmlentities($line)) . "<br />\n"; } ?> I used this to get all lines of a file, and in this case it was a html file. If you adjust the looping I'm sure you can print just the first line. Quote Link to comment https://forums.phpfreaks.com/topic/71269-read-only-1st-line-of-a-text-document/#findComment-358517 Share on other sites More sharing options...
sKunKbad Posted September 30, 2007 Share Posted September 30, 2007 This works: <?php $lines = file('http://www.yourfile.com'); foreach ($lines as $line_num => $line) { if ($line_num == 0){ echo str_replace(""," ",htmlentities($line)) . "<br />\n"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/71269-read-only-1st-line-of-a-text-document/#findComment-358521 Share on other sites More sharing options...
Cagecrawler Posted September 30, 2007 Share Posted September 30, 2007 Yes, but that loads the whole file into the array to start off with, which is what the OP is currently doing and wants to avoid. Quote Link to comment https://forums.phpfreaks.com/topic/71269-read-only-1st-line-of-a-text-document/#findComment-358524 Share on other sites More sharing options...
php_joe Posted September 30, 2007 Author Share Posted September 30, 2007 Can't you use fgets()? http://www.php.net/manual/en/function.fgets.php <?php $handle = fopen("file.txt", "r"); $firstline = fgets($handle); echo $firstline; ?> Thank you! This looks like what I'm looking for. Quote Link to comment https://forums.phpfreaks.com/topic/71269-read-only-1st-line-of-a-text-document/#findComment-358543 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.