469,323 Members | 1,558 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

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 8118
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

Post your reply

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

Similar topics

4 posts views Thread by ralphNOSPAM | last post: by
3 posts views Thread by Pir8 | last post: by
26 posts views Thread by SL33PY | last post: by
1 post views Thread by Thomas Kowalski | last post: by
4 posts views Thread by Neil.Smith | last post: by
2 posts views Thread by hzgt9b | last post: by
13 posts views Thread by Chris Carlen | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.