Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help needed with displaying the contents of a text file. 1

Status
Not open for further replies.

Giddygoat

Programmer
May 16, 2001
36
GB
Hello

I have a text file which contains some data I want to display in a HTML page. The file looks like this...

[senario]Type senario here
[who]Who said it
[what]What was said
[who]Who said it
[what]What was said
[comment]What a load of rubbish

I want to use PHP to parse the text file and display the text in a HTML table line this

<table>
<tr>
<td colspan=&quot;2&quot;>senario line</td>
</tr>
<tr>
<td>who line</td>
<td>what line</td>
</tr>
....repeated
<tr>
<td colspan=&quot;2&quot;>comment line</td>
</tr>
</table>

Can anyone help with the reading the file and stripping out the tags??

Thanks in Advance

John
 
Is there only one &quot;block&quot; of data in the text file, or many? if there are many, how are they separated? (i.e., blank line, no separation, etc.) Brad Gunsalus
Cymtec Systems, Inc.
bgunsalus@cymtec.com
 
Okay, well, the top portion reads all your lines into an array, discarding any empty lines just in case that is how you separate your blocks of data (if there are more than 1).
The bottom is an example of how to get the data back out. You may need to place the table inside a loop if you have more than one block of values.


$myfile = fopen($myfilename, &quot;r&quot;);
while (!feof($myfile)) {
$myline = fgets($myfile, 255);
$new_line = chop(substr($myline,1+strpos($myline,']')));
if (strcmp($new_line,'')){
array_push($values,$new_line);
}
}

print '<table>
<tr>
<td colspan=&quot;2&quot;>' . array_shift($values) . '</td>
</tr>
<tr>
<td>' . array_shift($values) . '</td>
<td>' . array_shift($values) . '</td>
</tr>

<tr>
<td colspan=&quot;2&quot;>' . array_shift($values) . '</td>
</tr>
</table>';



I'm sure there are ways to do this that are immensely slicker, but this should get you started. Brad Gunsalus
Cymtec Systems, Inc.
bgunsalus@cymtec.com
 
Thanks for the reply it is very helpful.

I will have a lot of the tagged segments in my text file, so I will need a mechanism for adding multiple rows into the table.

I dont have a very good handle on how PHP and HTML work together. I presume that I just add some PHP inbetween my <table> & </table> tags.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top