Forum Moderators: coopster

Message Too Old, No Replies

PHP Include Single Line of a File

only want to include line 1 of a file

         

cookiemonster

11:19 pm on Jun 2, 2010 (gmt 0)

10+ Year Member



Hi,

I have a file which I want to include via PHP, however I only want to include the first line of the file. Breaking up the file is not currently an option.
Is there any way that I can only include one line of a file using PHP?

PS. The file to be included does not necessarily need to be a .PHP file - it can be .txt, etc so options such as readfile() are welcome.

Thanks in advance!

Frank_Rizzo

12:02 pm on Jun 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



fgets() will read a file line by line.


<?php
$myfile = "test.txt";
$file = fopen($myfile, "r") or exit("Unable to open file!");

$lines_read = 0;
$first_line = '';

while(!feof($file) && $lines_read < 1) {
$lines_read++;
$first_line = fgets($file);
}
fclose($file);

if($lines_read == 1) {
echo "The first line of $myfile reads: $first_line";
} else {
echo "Could not read from file";
}

?>

Readie

10:22 am on Jun 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Much more simple:

$file = file('/path/to/file.txt');
echo $file[0];

cookiemonster

11:55 am on Jun 4, 2010 (gmt 0)

10+ Year Member



Thank you, Readie - you have saved my day yet again!

Readie

3:33 pm on Jun 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



:) happy to help.

An improvement on my code by the way:

echo ($file = file('path/to/file.php', FILE_IGNORE_NEW_LINES))? $file[0] : 'Unable to read file';

Error on failure, removes the \n from the end of the string.

penders

7:14 pm on Jun 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



FILE_IGNORE_NEW_LINES


I've found this flag doesn't work too well under windows, when the file being read has \r\n line endings. Instead use rtrim() on the result to remove the line ending.

cookiemonster

8:12 pm on Jun 4, 2010 (gmt 0)

10+ Year Member



Readie, thanks for the improvement, but I won't be using it -
Returning nothing on error was actually a bonus for my specific situation, and the \n was no problem for me.

Readie

8:46 pm on Jun 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, you can remove the error message - it's better code, as you won't return an E_NOTICE undefined index when it fails.

Penders:

I didn't know that FILE_IGNORE_NEW_LINES failed on Windows - so thanks for that.

My personal servers, and all the servers we run at work are Linux, so I can take that liberty though :)

cookiemonster

9:06 pm on Jun 4, 2010 (gmt 0)

10+ Year Member



Hmmm .... alrighty, I'll try it out later and let you know how it goes.

rocknbil

4:39 pm on Jun 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've found this flag doesn't work too well under windows


I mentioned this the other day in this thread [webmasterworld.com] (see PHP comments,) and the test was on a 'nix server . . . but it might be something I was doing wrong <shrug>.

Readie

5:46 pm on Jun 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



 $len = count($fields); // So Readie doesn't yell at me. :-) 

Lol :)

I sort of jumped to the end of that thread when I read it the first time ^^

--- EDIT

Re-reading that code and the comments you wrote on that thread rock, did you try this?:

$file = ('/path/to/file.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

rocknbil

6:15 pm on Jun 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nah, project for another day. What was weird is rtrim() didn't seem to help either.