faqts : Computers : Programming : Languages : PHP : Common Problems : Files

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

5 of 7 people (71%) answered Yes
Recently 4 of 6 people (67%) answered Yes

Entry

I wish to capture colon-delimited data from a txt file and to break it into lines and from there columns to format into an HTML table. How?

Sep 7th, 2004 02:06
Karl Vegar Larsen, Jon Thomas, http://www.php.net


//Start opening that file usingn fopen
$fh=fopen("filename", "r");
// Lets initiate a table i a variable.
$table="<TABLE>";
// Add headings as you like
$table.="<TR><TH>Heading 1</TH><TH>Heading 2</TH></TR>";
// Loop though the file
while(!eof($fh)){
 // This will be a new line in the table.
 $table.="<TR>";
 // Now we get the file a line at a time.
 $line=fread($fh);
 // Lets split up that line.
 $field=explode(";", $line);
 // Loop through that array.
 for($t=0; $t<count($field); $t++){
  // Add the fields to the table.
  $table.="<TD>$field[$t]</TD>";
 } // End looping through the array
 $table.="</TR>"; // End the table row
} // End looping through the file
// Now end that table
$table.="</TABLE>";
// Now make the rest of the page.
echo "<HTML>";
echo "<HEAD><TITLE>Table page</TITLE></HEAD>";
echo "<BODY>";
// Display the table
echo $table;
// Finish the page.
echo "</BODY></HTML>";