Advanced php include
Moderator: General Moderators
-
digitaldan
- Forum Newbie
- Posts: 9
- Joined: Wed Oct 17, 2007 4:04 am
Advanced php include
How do you include only a certain line from a text file using the php include command?
Thanks in advance.
Thanks in advance.
Last edited by digitaldan on Tue Nov 27, 2007 4:05 am, edited 3 times in total.
- CoderGoblin
- DevNet Resident
- Posts: 1425
- Joined: Tue Mar 16, 2004 10:03 am
- Location: Aachen, Germany
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.
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
- CoderGoblin
- DevNet Resident
- Posts: 1425
- Joined: Tue Mar 16, 2004 10:03 am
- Location: Aachen, Germany
-
digitaldan
- Forum Newbie
- Posts: 9
- Joined: Wed Oct 17, 2007 4:04 am
Basically I need to display a file with two lines handled separately.
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.
Code: Select all
11.79
Last Updated: 26/11/07 12:15PMThis file is dumped from a program which is then uploaded via FTP as a scheduled task.
Thanks for your insight so far.
- CoderGoblin
- DevNet Resident
- Posts: 1425
- Joined: Tue Mar 16, 2004 10:03 am
- Location: Aachen, Germany
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.
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
-
digitaldan
- Forum Newbie
- Posts: 9
- Joined: Wed Oct 17, 2007 4:04 am
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
If you are attempting to read the file into an array line by line, then use file() instead (no need to explode on newlines)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.
Precisely. file() it. Gets everything line by line by line =]Jcart wrote:If you are attempting to read the file into an array line by line, then use file() instead (no need to explode on newlines)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.
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
feyd | Please use
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.
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]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 />';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' ) );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]- aaronhall
- DevNet Resident
- Posts: 1040
- Joined: Tue Aug 13, 2002 5:10 pm
- Location: Back in Phoenix, missing the microbrews
- Contact:
array_slice() might be a little easier