473,323 Members | 1,537 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,323 software developers and data experts.

Parsing text file with PHP

15
In the code, I have bold text where I have a question.
I am not sure how to parse the file into different fields (right below with bold: machine,plan,study,scantype, date)... and I have more different files in the same format of this below.

Others also have machine,plan, study, scan type, date as their information.
right side of colon (:) have to be parsed separately..

Machine: 225kV_30cmTT_RmtRecon_30UG6003
Plan: TIM_BONE
Study: MCZ_35677_20100118-142011
Scan Type: AxialCT
Date: 2010.01.18 14:00:39


But I have no idea how to do this, can anyone help me out with this?

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. //Change location to be where you put the file
  4.  
  5. $file = fopen("C:\wamp\www\Other.txt", "r") or exit("Unable to 
  6.  
  7. open file!");
  8.  
  9. //Output a line of the file until the end is reached
  10.  
  11. while(!feof($file)){
  12. echo fgets($file). "<br />";
  13.  
  14. //This is where you will need to parse through the file looking for the colon
  15. //And put the part after the colon into a variable as well as on  the screen
  16.  
  17. }
  18. fclose($file);
  19.  
  20. ?>
  21.  
Apr 5 '10 #1

✓ answered by chathura86

it is because of the

High-Resolution CT Scans
Center for Quantitative Imaging
Penn State University

section

modify it to check if the second part exists

Expand|Select|Wrap|Line Numbers
  1. //explode from the :
  2.         $fields = explode(":", $line, 2);
  3.  
  4.         echo "<b>$fields[0]</b>";
  5.  
  6. //checks if second part exists 
  7.         if (isset($fields[1]))
  8.             echo " : $fields[1]";
  9.  
  10.         echo "<br/>";
  11.  

18 8648
chathura86
227 100+
you can use the php explode() function

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.     $file = fopen("info.txt", "r") or exit("Unable to open file!");
  4.  
  5.     while(!feof($file))
  6.     {
  7.         $line = fgets($file); // Read a line.
  8.  
  9.         //escape if the line is empty
  10.         if (trim($line) == "")
  11.             continue;
  12.  
  13.         //explode from the :
  14.         $fields = explode(":", $line, 2);
  15.     }
  16.  
  17.     fclose($file);
  18. ?>
  19.  
Regards
Apr 5 '10 #2
lka527
15
After I added

echo fgets($file). "<br />";

at the end of the php code I only got partial result in the page.
I got this below
Plan: TIM_BONE_NEW
Scan Type: AxialCT
I don't know why I am getting only partial data and I don't understand '2' in the php code you have given.. I am not really good at php and I really appreciate your help :)




Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.     $file = fopen("C:\wamp\www\Other.txt", "r") or exit
  4.  
  5. ("Unable to open file!");
  6.  
  7. while(!feof($file))
  8. {
  9.  
  10.     $line = fgets($file); // Read a line.
  11.  
  12.         //escape if the line is empty
  13.         if (trim($line) == "")
  14.             continue;
  15.  
  16.         //explode from the :
  17.         $fields = explode(":", $line, "2");
  18.  
  19.  
  20.     echo fgets($file). "<br />";
  21.  
  22.     }
  23.  
  24.     fclose($file);
  25. ?>
Apr 5 '10 #3
chathura86
227 100+
it is not appropriate to use

echo fgets($file). "<br />";

because what it does it read a line form the file, so next time when you
call it, it will read the next line.

so since you are reading a line in the while loop and also reading a it inside the
loop it will miss some lines.

read the fgets() in php manual for more details

Regards
Apr 5 '10 #4
lka527
15
Sir....
I am sorry that i keep asking you..
but in order to print every value how can I write this?
This is desperate for me:(
I tried to do this in array too but so confusing me..
Please let me know how to fix this into the better way.
Apr 5 '10 #5
chathura86
227 100+
for each iteration $fields array holds the two parts of that line

eg.

iteration 1

$fields[0] holds Machine and
$fields[1] holds 225kV_30cmTT_RmtRecon_30UG6003

iteration 2

$fields[0] holds Plan and
$fields[1] holds TIM_BONE

...

so every time you have an array with two values

Regards
Apr 5 '10 #6
Atli
5,058 Expert 4TB
Hey.

The fgets function fetches a single line from the file. Your first example showed how you can use it to fetch every line in the file in a loop; one after another.

What you need to do is; rather than echo the line, you should put it into a variable. Then you can use the explode function to split the line in the variable into two pieces; the one before the colon and the one after it.

After that you can do whatever you need to with each piece. This example shows how we typically split a line into pieces:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // Suppose this is one line of your file.
  3. $line = "Key: value";
  4.  
  5. // To get each side of the colon into separate
  6. // variables, you typically do something like:
  7. list($key, $value) = explode(": ", $line);
  8.  
  9. // After which you can use the variables
  10. // as you see fit.
  11. echo "The key: $key - The value: $value";
  12. ?>
Remember, your original code already fetches each line. All you need to do is store the line returned by the fgets() function and apply the above method to it.
Apr 5 '10 #7
chathura86
227 100+
@Atli
nice explanation

remember to put explode(":", $line, 2); 2 as the limit
because some of the rows contains : in the value also (Eg. 14:00:39)

Regards,
Apr 5 '10 #8
lka527
15
Thank you so much for your effort..
I am almost there, I think!

"Remember, your original code already fetches each line. All you need to do is store the line returned by the fgets() function and apply the above method to it."

You said this in the last part of your answer..
how can I store the line returned by the fgets() function?...
.. I just have no idea...T_T

This is what I got so far,
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.     $file = fopen("C:\wamp\www\Other.txt", "r") or exit
  4.  
  5. ("Unable to open file!");
  6.  
  7. while(!feof($file))
  8. {
  9.  
  10.     $line = fgets($file); // Read a line.
  11.  
  12.  
  13.        //escape if the line is empty
  14.         if (trim($line) == "")
  15.             continue;
  16.  
  17.         //explode from the :
  18.         $fields = explode(":", $line, "2");
  19.  
  20.     list($key,$value) = explode (": ",$line);
  21.     echo "the key: $key - the value: $value";
  22.  
  23.     }
  24.  
  25.     fclose($file);
  26. ?>
I don't think this is right ... but what I tried is this..
I hope this is the last question I asked..:-(
Apr 5 '10 #9
chathura86
227 100+
you dont have to use the both

Expand|Select|Wrap|Line Numbers
  1. //explode from the :
  2. $fields = explode(":", $line, 2);
  3.  
  4. list($key,$value) = explode (": ",$line);
  5.  
both lines does the same

use
Expand|Select|Wrap|Line Numbers
  1. list($key,$value) = explode (": ",$line, 2);
  2. echo "the key: $key - the value: $value";
  3.  
or

Expand|Select|Wrap|Line Numbers
  1. $fields = explode(":", $line, 2);
  2. echo "the key: $fields[0] - the value: $fields[1]";
  3.  
regards
Apr 5 '10 #10
lka527
15
I asked another question below.. Please review this and answer for me :(
Thank you so much
Apr 5 '10 #11
lka527
15
Notice: Undefined offset: 1 in C:\wamp\www\test.php on line 18
the key: High-Resolution CT Scans - the value:
Notice: Undefined offset: 1 in C:\wamp\www\test.php on line 18
the key: Center for Quantitative Imaging - the value:
Notice: Undefined offset: 1 in C:\wamp\www\test.php on line 18
the key: Penn State University - the value: the key: Machine - the value: PSU_225kV_30cmTT_RmtRecon_30UG6003 the key: Plan - the value: TIM_BONE_NEW the key: Study - the value: MCZ_35677_H_MIDSHAFT_20100118-142011 the key: Scan Type - the value: AxialCT the key: Date - the value: 2010.01.18 14:00:39 the key: Rows to Collect - the value: 139 the key: Energy Settings - the value: 180 kV, 0.11 mA the key: SOD - the value: 134.999 mm the key: Views/Average - the value: 1440 / 4 the key: Scale/Offset - the value: 45 / 1500 the key: Field of View - the value: 41.47 mm
Notice: Undefined offset: 1 in C:\wamp\www\test.php on line 18
the key: x = y = 0.040498046875 mm - the value:
Notice: Undefined offset: 1 in C:\wamp\www\test.php on line 18
the key: z = 0.043 mm - the value: the key: Matrix Size - the value: 1024x1024 the key: Number of Slices - the value: 1 16bit Tiff Images the key: Corrective Options - the value: the key: Number of Sectors in Polar Array - the value: 60 the key: Image Noise Threshold - the value: 200 the key: Ring Component Threshold - the value: 6000 the key: High Pass Adaptive Filter Length - the value: 11 the key: Low Pass Filter Distance - the value: 10 the key: Low Pass Number of Bindings - the value: 10


Alright,
this is my last question hopefully..
This result is what I just got when I replaced the thing as you told me to replace in the last reply.

I don't know what I should do now...:-(
I actually had more data than I posted in the first one..
Apr 5 '10 #12
chathura86
227 100+
$line holds the line

echo $line will print the line

Regards
Apr 5 '10 #13
chathura86
227 100+
can you attach the file?
Apr 5 '10 #14
lka527
15
http://personal.psu.edu/kvl5046/Other.txt

is the text file I am supposed to parse..



Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.     $file = fopen("C:\wamp\www\Other.txt", 
  4.  
  5. "r") or exit
  6.  
  7. ("Unable to open file!");
  8.  
  9. while(!feof($file))
  10. {
  11.  
  12.     $line = fgets($file); // Read a line.
  13.  
  14.  
  15.      //escape if the line is empty
  16.         if (trim($line) == "")
  17.             continue;
  18.  
  19.     $fields = explode(":", $line, "2");
  20. echo "<b>$fields[0]</b>".": $fields
  21.  
  22. [1]"."<br/>";
  23.  
  24.  
  25.  
  26.     }
  27.  
  28.     fclose($file);
  29. ?>
this is what I actually have now
Apr 5 '10 #15
lka527
15
http://www.personal.psu.edu/kvl5046/test1.jpg

is the actual result screen shot I just took..
Apr 5 '10 #16
chathura86
227 100+
it is because of the

High-Resolution CT Scans
Center for Quantitative Imaging
Penn State University

section

modify it to check if the second part exists

Expand|Select|Wrap|Line Numbers
  1. //explode from the :
  2.         $fields = explode(":", $line, 2);
  3.  
  4.         echo "<b>$fields[0]</b>";
  5.  
  6. //checks if second part exists 
  7.         if (isset($fields[1]))
  8.             echo " : $fields[1]";
  9.  
  10.         echo "<br/>";
  11.  
Apr 5 '10 #17
lka527
15
oh!!!!!! it works now!
Thank you so much!!!!!!!!!!!!!!!!!!!!!!!!!

I will post more questions as I have .
I appreciate your help so much!!!!!!!!!!!!!!

You are my hero:)
Apr 5 '10 #18

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

Similar topics

4
by: ralphNOSPAM | last post by:
Is there a function or otherwise some way to pull out the target text within an XML tag? For example, in the XML tag below, I want to pull out 'CALIFORNIA'. ...
3
by: Pir8 | last post by:
I have a complex xml file, which contains stories within a magazine. The structure of the xml file is as follows: <?xml version="1.0" encoding="ISO-8859-1" ?> <magazine> <story>...
26
by: SL33PY | last post by:
Hi, I'm having a problem parsing strings (comming from a flat text input file) to doubles. the code: currentImportDetail.Result = CType(line.Substring(7, 8).Trim(" "), System.Double) What...
1
by: Thomas Kowalski | last post by:
Hi, I have to parse a plain, ascii text file (on local HD). Since the file might be many millions lines long I want to improve the efficiency of my parsing process. The resulting data structure...
4
by: Neil.Smith | last post by:
I can't seem to find any references to this, but here goes: In there anyway to parse an html/aspx file within an asp.net application to gather a collection of controls in the file. For instance...
3
by: toton | last post by:
Hi, I have some ascii files, which are having some formatted text. I want to read some section only from the total file. For that what I am doing is indexing the sections (denoted by .START in...
2
by: hzgt9b | last post by:
I've written a simple javascript page that parses an XML file... (Actually I just modified the "Parsing an XML File" sample from http://www.w3schools.com/dom/dom_parser.asp) The page works great...
13
by: Chris Carlen | last post by:
Hi: Having completed enough serial driver code for a TMS320F2812 microcontroller to talk to a terminal, I am now trying different approaches to command interpretation. I have a very simple...
1
by: martinsson | last post by:
Hi all! I'm pretty mad about this... dont know what is going on. Im parsing XML file that looks like this: <something> __<item att="something">text<item> __<item...
2
by: python | last post by:
I'm parsing a text file for a proprietary product that has the following 2 directives: #include <somefile> #define <name<value> Defined constants are referenced via <#name#syntax. I'm...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.