473,398 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Read lines from text file using PHP

16
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 =)
Jun 11 '07 #1
15 7131
ak1dnar
1,584 Expert 1GB
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 =)
Hi, Fab108 Welcome to TSDN.

Try these Tutorials
Jun 11 '07 #2
Fab108
16
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?
Jun 12 '07 #3
ak1dnar
1,584 Expert 1GB
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?
I 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:
Expand|Select|Wrap|Line Numbers
  1. Mike Johann | 15-02-2005 | mike@somewhere.com
  2. Jessica Moore | 20-10-2000 | jess@elesewhere.com 
  3.  
  4.  
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?
Jun 12 '07 #4
r035198x
13,262 8TB
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?
Use the gets function then .
Jun 12 '07 #5
Fab108
16
I 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:
Expand|Select|Wrap|Line Numbers
  1. Mike Johann | 15-02-2005 | mike@somewhere.com
  2. Jessica Moore | 20-10-2000 | jess@elesewhere.com 
  3.  
  4.  
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..haha
Jun 12 '07 #6
ak1dnar
1,584 Expert 1GB
Is this the structure of your text file or differ than this?

Expand|Select|Wrap|Line Numbers
  1. Datalogs:
  2. Date: 2007/6/11
  3. Time: 13:9:3
  4. Device ID: B108
  5. About total Numbers: 001891
  6. ===================
  7. =====================
  8. Record: 1
  9. Event Type: 3
  10. Year: 2007
  11. Month: 6
  12. Day: 7
  13. Hour: 5
  14. Minute: 9
  15. Second: 37.527237
  16. Record: 2
  17. Event Type: 3
  18. Year: 2007
  19. Month: 6
  20. Day: 7
  21. Hour: 5
  22. Minute: 9
  23. Second: 37.527237
  24. Record: 3
  25. Event Type: 3
  26. Year: 2007
  27. Month: 6
  28. Day: 7
  29. Hour: 5
  30. Minute: 9
  31. Second: 37.527237
And what you meant by saying "year onwards" ?
Jun 12 '07 #7
Fab108
16
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.
Jun 13 '07 #8
ak1dnar
1,584 Expert 1GB
text.txt
Expand|Select|Wrap|Line Numbers
  1. Datalogs:
  2. Date: 2007/6/11
  3. Time: 13:9:3
  4. Device ID: Data Logger
  5. About total Numbers: 001891
  6. ===================
  7. =====================
  8. Record: 1
  9. Event Type: 3
  10. Year: 2007
  11. Month: 6
  12. Day: 7
  13. Hour: 5
  14. Minute: 9
  15. Second: 37.527237
  16. Latitude: 1.342335
  17. Longitude: 103.776921
  18. Altitude: 0.000000
  19. PDOP: 50.000000
  20. HDOP: 50.000000
  21. Satellite No: 3
  22. Speed(KMs/hr): 0.000000
  23. Direction: 0.000000
  24. =====================
  25. Record: 2
  26. Event Type: 1
  27. Year: 2007
  28. Month: 6
  29. Day: 7
  30. Hour: 5
  31. Minute: 9
  32. Second: 42.526936
  33. Latitude: 1.342367
  34. Longitude: 103.776861
  35. Altitude: 0.000000
  36. PDOP: 50.000000
  37. HDOP: 50.000000
  38. Satellite No: 3
  39. Speed(KMs/hr): 0.000000
  40. Direction: 0.000000
  41. =====================
  42. Record: 3
  43. Event Type: 1
  44. Year: 2007
  45. Month: 6
  46. Day: 7
  47. Hour: 5
  48. Minute: 9
  49. Second: 47.526634
  50. Latitude: 1.342375
  51. Longitude: 103.776858
  52. Altitude: 0.000000
  53. PDOP: 50.000000
  54. HDOP: 50.000000
  55. Satellite No: 3
  56. Speed(KMs/hr): 0.000000
  57. Direction: 0.000000
  58. =====================
  59.  
Try to modify this script as per your requirement.
read_text.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $filename = "text.txt";
  3. $fd = fopen ($filename, "r");
  4. $contents = fread ($fd,filesize ($filename));
  5. fclose ($fd);
  6.  
  7. $delimiter = "=====================";
  8. $splitcontents = explode($delimiter, $contents);
  9.  
  10. foreach( $splitcontents as $record )
  11. {
  12.  
  13. echo "$record<hr>";
  14. }
  15.  
  16. ?>
  17.  
Jun 13 '07 #9
Fab108
16
Thanks a lot, if i have anymore problem, i will pm you..= )
Jun 13 '07 #10
ak1dnar
1,584 Expert 1GB
Thanks a lot, if i have anymore problem, i will pm you..= )
Glad to hear that, Fab108 !
You are welcome ! anytime.
-Ajaxrand
Jun 13 '07 #11
r035198x
13,262 8TB
Thanks a lot, if i have anymore problem, i will pm you..= )
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.

Sorry for being a spoil sport but those are the rules.
Jun 13 '07 #12
ak1dnar
1,584 Expert 1GB
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.

Sorry for being a spoil sport but those are the rules.
Its my mistake, i really forgot to mention that in my last post.

Do NOT PM questions to individual Experts, Moderators or Administrators

Sorry r035198x.
Jun 13 '07 #13
Fab108
16
Ok i understand, haha i will post my questions if i have any = ) thanks a lot.
Jun 15 '07 #14
Fab108
16
Ok..i got a new problem. After i read the lines, how i convert the data and put into a database?
Jun 19 '07 #15
bonski
53
text.txt
Expand|Select|Wrap|Line Numbers
  1. Datalogs:
  2. Date: 2007/6/11
  3. Time: 13:9:3
  4. Device ID: Data Logger
  5. About total Numbers: 001891
  6. ===================
  7. =====================
  8. Record: 1
  9. Event Type: 3
  10. Year: 2007
  11. Month: 6
  12. Day: 7
  13. Hour: 5
  14. Minute: 9
  15. Second: 37.527237
  16. Latitude: 1.342335
  17. Longitude: 103.776921
  18. Altitude: 0.000000
  19. PDOP: 50.000000
  20. HDOP: 50.000000
  21. Satellite No: 3
  22. Speed(KMs/hr): 0.000000
  23. Direction: 0.000000
  24. =====================
  25. Record: 2
  26. Event Type: 1
  27. Year: 2007
  28. Month: 6
  29. Day: 7
  30. Hour: 5
  31. Minute: 9
  32. Second: 42.526936
  33. Latitude: 1.342367
  34. Longitude: 103.776861
  35. Altitude: 0.000000
  36. PDOP: 50.000000
  37. HDOP: 50.000000
  38. Satellite No: 3
  39. Speed(KMs/hr): 0.000000
  40. Direction: 0.000000
  41. =====================
  42. Record: 3
  43. Event Type: 1
  44. Year: 2007
  45. Month: 6
  46. Day: 7
  47. Hour: 5
  48. Minute: 9
  49. Second: 47.526634
  50. Latitude: 1.342375
  51. Longitude: 103.776858
  52. Altitude: 0.000000
  53. PDOP: 50.000000
  54. HDOP: 50.000000
  55. Satellite No: 3
  56. Speed(KMs/hr): 0.000000
  57. Direction: 0.000000
  58. =====================
  59.  
Try to modify this script as per your requirement.
read_text.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $filename = "text.txt";
  3. $fd = fopen ($filename, "r");
  4. $contents = fread ($fd,filesize ($filename));
  5. fclose ($fd);
  6.  
  7. $delimiter = "=====================";
  8. $splitcontents = explode($delimiter, $contents);
  9.  
  10. foreach( $splitcontents as $record )
  11. {
  12.  
  13. echo "$record<hr>";
  14. }
  15.  
  16. ?>
  17.  
this is the same method your going to use with your new problem..

take not of this..

[PHP]$delimiter = "=====================";
$splitcontents = explode($delimiter, $contents);[/PHP]

this is how you extract it from text file...
delimiter is the one that separates your data...
in this format, the delimiter you going to extract is this $delimiter = "=====================";
Expand|Select|Wrap|Line Numbers
  1. Record: 2
  2. Event Type: 1
  3. Year: 2007
  4. Month: 6
  5. Day: 7
  6. Hour: 5
  7. Minute: 9
  8. Second: 42.526936
  9. Latitude: 1.342367
  10. Longitude: 103.776861
  11. Altitude: 0.000000
  12. PDOP: 50.000000
  13. HDOP: 50.000000
  14. Satellite No: 3
  15. Speed(KMs/hr): 0.000000
  16. Direction: 0.000000
  17. =====================
  18. Record: 3
  19. Event Type: 1
  20. Year: 2007
  21. Month: 6
  22. Day: 7
  23. Hour: 5
  24. Minute: 9
  25. Second: 47.526634
  26. Latitude: 1.342375
  27. Longitude: 103.776858
  28. Altitude: 0.000000
  29. PDOP: 50.000000
  30. HDOP: 50.000000
  31. Satellite No: 3
  32. Speed(KMs/hr): 0.000000
  33. Direction: 0.000000
results to this...

// first data... inside an array
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


// 2nd data inside an array
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


now... to extract each data inside.. so that it will fit in your table with their corresponding fields..
use delimiter $delimiter = "\n"; means new line..

[PHP]$delimiter = "\n";
$data_contents = explode($delimiter, $splitcontents[$i]);[/PHP]

//notice that $splitcontents[$i] is in array already.. 'cause your doing this with an array inside an array..

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


now the result is would line by line.. now use the same method.. but with a delimiter $delimiter = ':';

remember.. its an array within an array... have fun.. ^___^
Jun 19 '07 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Ruben | last post by:
Hello. I am trying to read a small text file using the readline statement. I can only read the first 2 records from the file. It stops at the blank lines or at lines with only spaces. I have a...
2
by: Garibaldi | last post by:
Thanks to all... I need to read a block of text, operate on the read text then move to the next text block and so on and so on from a 650MB file. Pseudo code would be something like this: ...
6
by: G.Esmeijer | last post by:
Friends, I would like to read a text file (fixed length formaated) really fast and store the data into an Access database (2003). Using the streamreader and reading line by line, separating the...
2
by: CsharpGuy | last post by:
I have to read a text file and parse it out to load to a db and I'm having some issues in doing it. here is what the text file looks like Dealership number: 98665362236 Location: Maryland...
3
by: Yaniv | last post by:
Hi I'm new in VB.NET. I wrote an application which opens a text file and read it all lines untill the EOF this file is open for read only and for sharing asllowed. every 5 seconds another...
3
by: Ray | last post by:
Hello World, I made a Windowsform that reads data from a CSV file. It works fine, but when I have read the data of a record I have to re-Debug the form to read another record. So when I put a...
1
by: ashok0866 | last post by:
I had created a macro to read data from an excel sheet and write the values to a text file. I had used "ActiveSheet.Range("GB" & k).Value" command to read the values from the excel. The issue...
1
by: godofredo | last post by:
Hi! I've a weird problem using ReadAllLines Function. I have a plain text file which has about 6500 lines, each one representing a 3D point. I read the text file and store it in a string array...
9
by: =?Utf-8?B?QnJpYW4gQ29vaw==?= | last post by:
I want to open a text file and format it into a specific line and then apply color to a specific location of the text and then display it in a RichTextBox after all of this is done. I can do all...
4
by: Keith G Hicks | last post by:
I'm trying to read a text file and alter the contents of specific lines in the file. I know how to use streamreader to read each line of a file. I'm doing that already to get the data into a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.