Page 1 of 1

Advanced php include

Posted: Mon Nov 26, 2007 6:16 am
by digitaldan
How do you include only a certain line from a text file using the php include command?

Thanks in advance.

Posted: Mon Nov 26, 2007 6:21 am
by CoderGoblin
To my knowledge you can't. The nearest I can come up with is to functionalise the code and call the function you are after the include. The real question is what are you actually trying to do where this would be a requirement ?

Edit...

rereading the question... to get a specific line of a text file... you don't use the include() command you need to process the file using fopen, fgets() or stream_get_line() which someone has mentioned as being far quicker in the comments.

Posted: Mon Nov 26, 2007 7:42 am
by digitaldan
How would I functionalise the code in a text file to then call in an include statement?

Cheers.

Posted: Mon Nov 26, 2007 7:46 am
by CoderGoblin
Easiest thing to do is for you to show an example of the file you wish to have a line from. I am still unsure if this file contains php commands or just text... More detailed explanation of requirements will normally lead to better answers. :wink:

Posted: Mon Nov 26, 2007 7:54 am
by digitaldan
Basically I need to display a file with two lines handled separately.

Code: Select all

11.79

Last Updated: 26/11/07 12:15PM
The first decimal value needs to be displayed using PHP include and the last updated date needs to be included separately later on in the web page. If it makes things better I can arrange for the developer of the program to make the dumped text file adjusted.

This file is dumped from a program which is then uploaded via FTP as a scheduled task.

Thanks for your insight so far.

Posted: Mon Nov 26, 2007 8:12 am
by CoderGoblin
If the file just contains those lines (only 3 lines long, never longer) I would use file_get_contents to read the entire file into a string. Once it is a string you could split it using string handling functions such as regular expressions, explode (on \n ?) or some other method to separate the complete string into the separate variables you require.

Key point to note is you are not using
php include() as you are not dealing with a php file that needs evaluating. Include as the php command always includes and evaluates everything.

Posted: Mon Nov 26, 2007 8:30 am
by digitaldan
I'm very new to PHP so I have little idea on handling once the file contents are in a string such as splitting it up line by line. It's a shame PHP doesn't have the ability to declare line numbers.

Could you provide some example on how to handle the file contents once in a string.

Thanks.

Posted: Mon Nov 26, 2007 8:40 am
by digitaldan
Wow.

Just found an excellent example.

http://discomoose.org/2006/04/28/readin ... -with-php/

Good?

Posted: Mon Nov 26, 2007 1:50 pm
by RobertGonzalez
What is going to determine what line(s) in the file you are going to show?

Posted: Mon Nov 26, 2007 2:21 pm
by John Cartwright
CoderGoblin wrote:If the file just contains those lines (only 3 lines long, never longer) I would use file_get_contents to read the entire file into a string. Once it is a string you could split it using string handling functions such as regular expressions, explode (on \n ?) or some other method to separate the complete string into the separate variables you require.
If you are attempting to read the file into an array line by line, then use file() instead (no need to explode on newlines)

Posted: Mon Nov 26, 2007 8:10 pm
by s.dot
Jcart wrote:
CoderGoblin wrote:If the file just contains those lines (only 3 lines long, never longer) I would use file_get_contents to read the entire file into a string. Once it is a string you could split it using string handling functions such as regular expressions, explode (on \n ?) or some other method to separate the complete string into the separate variables you require.
If you are attempting to read the file into an array line by line, then use file() instead (no need to explode on newlines)
Precisely. file() it. Gets everything line by line by line =]

Posted: Tue Nov 27, 2007 4:04 am
by digitaldan
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Code: Select all

<?php
		// file() loads the contents of file.txt into an array, $lines
		// each line in the file becomes a seperate element of the array.

		$lines = file ('../status_history.txt')

		// we can also access each line of the file seperately

		echo htmlspecialchars($lines[0]) . '<br />';
		echo htmlspecialchars($lines[1]) . '<br />';
		echo htmlspecialchars($lines[2]) . '<br />';
		echo htmlspecialchars($lines[3]) . '<br />';
How can I make it so that the last 3 lines of the text file are displayed instead of counting them all?

EDIT: It's ok. I've found that you can use array_reverse.

E.g.

Code: Select all

$lines = array_reverse( file( '../weight_history.txt' ) );
Thanks.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue Nov 27, 2007 7:26 am
by aaronhall
array_slice() might be a little easier