使用PHP从文本文件创建

使用PHP从文本文件创建

本文介绍了使用PHP从文本文件创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从文本文件中创建一个带有边框的表格(此文本文件每次在某人填写完表格后都会更新.一行,一个人):

I need to create a table with borders from the text file (this text file is updated every time when someone finishes filling a form. One row, one person):

Herard|TRO789|Suzuki|France|Gendolfina|Fresko|food|500|2015-04-25 14:40
Bob|MGA789|Mercedes|Latvia|Polaris|Dread|parts|1000|2015-04-26 16:15

我已经创建了一个脚本,该脚本可以分别读取每个单词,但是不知道如何将它们放到表中:

I've already created a script which reads every word separately, but don't know how to to put them to table:

<?php
$file = fopen("info.txt", "r") or die("Unable to open file!");
while (!feof($file)){
    $data = fgets($file);
    list($name, $number, $type, $country, $company, $gcompany, $supply, $weight, $datetime) = explode("|", $data);
    }
    fclose($failas);
?>

因此,我需要一个脚本,该脚本可以读取文本文件并创建一个表,该表的行数与文本文件的行数相同.

So I need a script which could read the text file and create a table with the same number of rows as the text file has.

推荐答案

使用str_replace|符号替换为HTML表单元格分隔符.

Use str_replace to replace the | signs with HTML table cell delimiters.

<?php
echo '<table border="1">';
$file = fopen("info.txt", "r") or die("Unable to open file!");
while (!feof($file)){
    $data = fgets($file);
    echo "<tr><td>" . str_replace('|','</td><td>',$data) . '</td></tr>';
}
echo '</table>';
fclose($file);
?>

这篇关于使用PHP从文本文件创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 15:10