Hello everyone, i am new here. Hmm...can you all please help me with this..what is the code for reading lines from a text file..Thanks =)
Read lines from text file using PHP
Collapse
X
-
Thanks for your help, but fgets help us to get the first line for example my text file is
Datalogs:
Date: 2007/6/11
Time: 13:9:3
Device ID: B108
About total Numbers: 001891
=============== ====
=============== ======
Record: 1
Event Type: 3
Year: 2007
Month: 6
Day: 7
Hour: 5
Minute: 9
Second: 37.527237
And i only want to display from Year onwards, how do i do that using fgets?Comment
-
I didn't ask to use just only the fgets().There were number of tutorials on the search result that i supplied.Originally posted by Fab108Thanks for your help, but fgets help us to get the first line for example my text file is
Datalogs:
Date: 2007/6/11
Time: 13:9:3
Device ID: B108
About total Numbers: 001891
=============== ====
=============== ======
Record: 1
Event Type: 3
Year: 2007
Month: 6
Day: 7
Hour: 5
Minute: 9
Second: 37.527237
And i only want to display from Year onwards, how do i do that using fgets?
And one more thing if you want to read this values from a text file there should be a specific format for each and every record.
example:
[code=text]
Mike Johann | 15-02-2005 | mike@somewhere. com
Jessica Moore | 20-10-2000 | jess@elesewhere .com
[/code]
for better readability.
The format that you have supplied also can read, but its not a god practice for creating a application. Still i can't understand whether it is a single file or a combination. and is this the only record you have?Comment
-
Use the gets function then .Originally posted by Fab108Thanks for your help, but fgets help us to get the first line for example my text file is
Datalogs:
Date: 2007/6/11
Time: 13:9:3
Device ID: B108
About total Numbers: 001891
=============== ====
=============== ======
Record: 1
Event Type: 3
Year: 2007
Month: 6
Day: 7
Hour: 5
Minute: 9
Second: 37.527237
And i only want to display from Year onwards, how do i do that using fgets?Comment
-
Originally posted by ajaxrandI didn't ask to use just only the fgets().There were number of tutorials on the search result that i supplied.
And one more thing if you want to read this values from a text file there should be a specific format for each and every record.
example:
[code=text]
Mike Johann | 15-02-2005 | mike@somewhere. com
Jessica Moore | 20-10-2000 | jess@elesewhere .com
[/code]
for better readability.
The format that you have supplied also can read, but its not a god practice for creating a application. Still i can't understand whether it is a single file or a combination. and is this the only record you have?
No i got 1891 records..= (..i am new to php that is why i am so vex now..hahaComment
-
Is this the structure of your text file or differ than this?
[CODE=text]Datalogs:
Date: 2007/6/11
Time: 13:9:3
Device ID: B108
About total Numbers: 001891
=============== ====
=============== ======
Record: 1
Event Type: 3
Year: 2007
Month: 6
Day: 7
Hour: 5
Minute: 9
Second: 37.527237
Record: 2
Event Type: 3
Year: 2007
Month: 6
Day: 7
Hour: 5
Minute: 9
Second: 37.527237
Record: 3
Event Type: 3
Year: 2007
Month: 6
Day: 7
Hour: 5
Minute: 9
Second: 37.527237[/CODE]
And what you meant by saying "year onwards" ?Comment
-
Datalogs:
Date: 2007/6/11
Time: 13:9:3
Device ID: Data Logger
About total Numbers: 001891
=============== ====
=============== ======
Record: 1
Event Type: 3
Year: 2007
Month: 6
Day: 7
Hour: 5
Minute: 9
Second: 37.527237
Latitude: 1.342335
Longitude: 103.776921
Altitude: 0.000000
PDOP: 50.000000
HDOP: 50.000000
Satellite No: 3
Speed(KMs/hr): 0.000000
Direction: 0.000000
=============== ======
Record: 2
Event Type: 1
Year: 2007
Month: 6
Day: 7
Hour: 5
Minute: 9
Second: 42.526936
Latitude: 1.342367
Longitude: 103.776861
Altitude: 0.000000
PDOP: 50.000000
HDOP: 50.000000
Satellite No: 3
Speed(KMs/hr): 0.000000
Direction: 0.000000
=============== ======
Record: 3
Event Type: 1
Year: 2007
Month: 6
Day: 7
Hour: 5
Minute: 9
Second: 47.526634
Latitude: 1.342375
Longitude: 103.776858
Altitude: 0.000000
PDOP: 50.000000
HDOP: 50.000000
Satellite No: 3
Speed(KMs/hr): 0.000000
Direction: 0.000000
=============== ======
This is the text file, from record 1, all the way to 1891.Comment
-
text.txt
[CODE=text]
Datalogs:
Date: 2007/6/11
Time: 13:9:3
Device ID: Data Logger
About total Numbers: 001891
=============== ====
=============== ======
Record: 1
Event Type: 3
Year: 2007
Month: 6
Day: 7
Hour: 5
Minute: 9
Second: 37.527237
Latitude: 1.342335
Longitude: 103.776921
Altitude: 0.000000
PDOP: 50.000000
HDOP: 50.000000
Satellite No: 3
Speed(KMs/hr): 0.000000
Direction: 0.000000
=============== ======
Record: 2
Event Type: 1
Year: 2007
Month: 6
Day: 7
Hour: 5
Minute: 9
Second: 42.526936
Latitude: 1.342367
Longitude: 103.776861
Altitude: 0.000000
PDOP: 50.000000
HDOP: 50.000000
Satellite No: 3
Speed(KMs/hr): 0.000000
Direction: 0.000000
=============== ======
Record: 3
Event Type: 1
Year: 2007
Month: 6
Day: 7
Hour: 5
Minute: 9
Second: 47.526634
Latitude: 1.342375
Longitude: 103.776858
Altitude: 0.000000
PDOP: 50.000000
HDOP: 50.000000
Satellite No: 3
Speed(KMs/hr): 0.000000
Direction: 0.000000
=============== ======
[/CODE]
Try to modify this script as per your requirement.
read_text.php
[CODE=php]
<?php
$filename = "text.txt";
$fd = fopen ($filename, "r");
$contents = fread ($fd,filesize ($filename));
fclose ($fd);
$delimiter = "============== =======";
$splitcontents = explode($delimi ter, $contents);
foreach( $splitcontents as $record )
{
echo "$record<hr >";
}
?>
[/CODE]Comment
-
Unfortunately we don't allow technical questions to be asked through PMs. we prefer it for them to be posted here where we all can learn from them.Originally posted by Fab108Thanks a lot, if i have anymore problem, i will pm you..= )
Sorry for being a spoil sport but those are the rules.Comment
-
Its my mistake, i really forgot to mention that in my last post.Originally posted by r035198xUnfortunately we don't allow technical questions to be asked through PMs. we prefer it for them to be posted here where we all can learn from them.
Sorry for being a spoil sport but those are the rules.
Do NOT PM questions to individual Experts, Moderators or Administrators
Sorry r035198x.Comment
Comment