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? -
<?php
-
-
//Change location to be where you put the file
-
-
$file = fopen("C:\wamp\www\Other.txt", "r") or exit("Unable to
-
-
open file!");
-
-
//Output a line of the file until the end is reached
-
-
while(!feof($file)){
-
echo fgets($file). "<br />";
-
-
-
//This is where you will need to parse through the file looking for the colon
-
//And put the part after the colon into a variable as well as on the screen
-
-
}
-
fclose($file);
-
-
?>
-
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 -
//explode from the :
-
$fields = explode(":", $line, 2);
-
-
echo "<b>$fields[0]</b>";
-
-
//checks if second part exists
-
if (isset($fields[1]))
-
echo " : $fields[1]";
-
-
echo "<br/>";
-
18 8515
you can use the php explode() function -
<?php
-
-
$file = fopen("info.txt", "r") or exit("Unable to open file!");
-
-
while(!feof($file))
-
{
-
$line = fgets($file); // Read a line.
-
-
//escape if the line is empty
-
if (trim($line) == "")
-
continue;
-
-
//explode from the :
-
$fields = explode(":", $line, 2);
-
}
-
-
fclose($file);
-
?>
-
Regards
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 :) - <?php
-
-
$file = fopen("C:\wamp\www\Other.txt", "r") or exit
-
-
("Unable to open file!");
-
-
while(!feof($file))
-
{
-
-
$line = fgets($file); // Read a line.
-
-
//escape if the line is empty
-
if (trim($line) == "")
-
continue;
-
-
//explode from the :
-
$fields = explode(":", $line, "2");
-
-
-
echo fgets($file). "<br />";
-
-
}
-
-
fclose($file);
-
?>
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
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.
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
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: - <?php
-
// Suppose this is one line of your file.
-
$line = "Key: value";
-
-
// To get each side of the colon into separate
-
// variables, you typically do something like:
-
list($key, $value) = explode(": ", $line);
-
-
// After which you can use the variables
-
// as you see fit.
-
echo "The key: $key - The value: $value";
-
?>
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.
@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,
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, -
<?php
-
-
$file = fopen("C:\wamp\www\Other.txt", "r") or exit
-
-
("Unable to open file!");
-
-
while(!feof($file))
-
{
-
-
$line = fgets($file); // Read a line.
-
-
-
//escape if the line is empty
-
if (trim($line) == "")
-
continue;
-
-
//explode from the :
-
$fields = explode(":", $line, "2");
-
-
list($key,$value) = explode (": ",$line);
-
echo "the key: $key - the value: $value";
-
-
}
-
-
fclose($file);
-
?>
I don't think this is right ... but what I tried is this..
I hope this is the last question I asked..:-(
you dont have to use the both -
//explode from the :
-
$fields = explode(":", $line, 2);
-
-
list($key,$value) = explode (": ",$line);
-
both lines does the same
use -
list($key,$value) = explode (": ",$line, 2);
-
echo "the key: $key - the value: $value";
-
or -
$fields = explode(":", $line, 2);
-
echo "the key: $fields[0] - the value: $fields[1]";
-
regards
I asked another question below.. Please review this and answer for me :(
Thank you so much
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..
$line holds the line
echo $line will print the line
Regards
http://personal.psu.edu/kvl5046/Other.txt
is the text file I am supposed to parse.. - <?php
-
-
$file = fopen("C:\wamp\www\Other.txt",
-
-
"r") or exit
-
-
("Unable to open file!");
-
-
while(!feof($file))
-
{
-
-
$line = fgets($file); // Read a line.
-
-
-
//escape if the line is empty
-
if (trim($line) == "")
-
continue;
-
-
$fields = explode(":", $line, "2");
-
echo "<b>$fields[0]</b>".": $fields
-
-
[1]"."<br/>";
-
-
-
-
}
-
-
fclose($file);
-
?>
this is what I actually have now
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 -
//explode from the :
-
$fields = explode(":", $line, 2);
-
-
echo "<b>$fields[0]</b>";
-
-
//checks if second part exists
-
if (isset($fields[1]))
-
echo " : $fields[1]";
-
-
echo "<br/>";
-
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:)
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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"...
|
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 =...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
|
by: jalbright99669 |
last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
| |