Advanced php include

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
digitaldan
Forum Newbie
Posts: 9
Joined: Wed Oct 17, 2007 4:04 am

Advanced php include

Post by digitaldan »

How do you include only a certain line from a text file using the php include command?

Thanks in advance.
Last edited by digitaldan on Tue Nov 27, 2007 4:05 am, edited 3 times in total.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
digitaldan
Forum Newbie
Posts: 9
Joined: Wed Oct 17, 2007 4:04 am

Post by digitaldan »

How would I functionalise the code in a text file to then call in an include statement?

Cheers.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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:
digitaldan
Forum Newbie
Posts: 9
Joined: Wed Oct 17, 2007 4:04 am

Post 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.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
digitaldan
Forum Newbie
Posts: 9
Joined: Wed Oct 17, 2007 4:04 am

Post 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.
digitaldan
Forum Newbie
Posts: 9
Joined: Wed Oct 17, 2007 4:04 am

Post by digitaldan »

Wow.

Just found an excellent example.

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

Good?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

What is going to determine what line(s) in the file you are going to show?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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 =]
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
digitaldan
Forum Newbie
Posts: 9
Joined: Wed Oct 17, 2007 4:04 am

Post 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]
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

array_slice() might be a little easier
Post Reply