473,545 Members | 1 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Read lines from text file using PHP

16 New Member
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 7139
ak1dnar
1,584 Recognized Expert Top Contributor
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 New Member
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 Recognized Expert Top Contributor
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 MVP
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 New Member
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 Recognized Expert Top Contributor
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 New Member
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 Recognized Expert Top Contributor
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 New Member
Thanks a lot, if i have anymore problem, i will pm you..= )
Jun 13 '07 #10

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

Similar topics

6
4263
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 while statement checking for an empty string "" which I understand represents an EOF in Python. The text file has some blank lines with spaces and...
2
510
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: 1. Start at the beginning of the text file 2. Read in 100K of text 3. Work with the 100K of text
6
23757
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 line into string just takes to long. When I Import the file with access manually goes fast. But how to use this fromout a C# programme who has...
2
1464
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 Owner: John Smith Type: Luxury BMW, 325i, VIN12363625252362, New,, 20051212. 35400, Ocoonner Lexus, IS300, VIN6936363633363. Used, Small dent on...
3
4204
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 applications write another line to this file at the end and I'm trying to read those new lines from my applications. the problem is that the EOF...
3
1798
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 new seek item in the textbox after I searched for an item and click the seek button.. it seems it is doing nothing. And also... what code can I use...
1
22170
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 is: some cells in the excel sheet is having two lines value and four lines data, the text file is generating that value in a single line. (Ex. the...
1
3378
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 using ReadAllLines(). The thing is that the first time I call the function it only reads about 512 lines and stores them in the array. Second time it...
9
2971
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 of the above after the file is loaded into the RichTextBox, and I am trying to speed the process up by doing it in a temp file.
4
3385
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 database. What I need help with is on how to locate a specific line in the file, change it and then save the updated text file. Can anyone help me out or...
0
7467
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7401
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7756
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5971
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4944
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3450
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3442
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
703
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.