简体   繁体   English

从文本文件中读取数据并使用php形成表格

[英]Reading data from text file and forming a table using php

I have a text file that contains data that looks like being formatted in a table. 我有一个文本文件,其中包含看起来像在表格中格式化的数据。 But they are just ordered lines of text made to resemble a table. 但它们只是有序的文字行,类似于一张桌子。 I am trying to read the text file, get only some of data and form a HTML table. 我试图读取文本文件,只获取一些数据并形成一个HTML表。

The text file looks like : 文本文件如下所示:

Class 1:
S.No RollNumber Name RandomAllocatedNumber Overall Result
---- ---------- ---- --------------------- --------------
1 ABC-BYT-M56-8T Sam Jackson NBV26173GHS Pass
2 BNS-SUD-H72-7Y Mario Javi  HAS12824SAD Pass

Class 2:
S.No RollNumber Name RandomAllocatedNumber Overall Result
---- ---------- ---- --------------------- --------------
1 POW-AVE-S36-7C Matt Stepson GSA22343GFS Pass
2 EWG-JAS-T12-3R Taylor Xavier  EWF54524EAD Pass

I used this code to read the complete file and display the output: 我用这段代码读取完整的文件并显示输出:

<?php
foreach(glob(somefile.txt") as $filename) {
$file = $filename;
$contents = file($file);
$string = implode("<br>",$contents);
echo $string;
echo "<br></br>";
}
?>

But I need to get only Student number, roll number and RandomAllocatedNumber from the above data. 但我需要从上面的数据中只获得学号,滚动号和RandomAllocatedNumber。

Which would look something like: 看起来像这样:

ClassNo |RollNumber     |RandomAllocatedNumber

1       |ABC-BYT-M56-8T |NBV26173GHS
1       |BNS-SUD-H72-7Y |HAS12824SAD
2       |POW-AVE-S36-7C |GSA22343GFS
2       |EWG-JAS-T12-3R |EWF54524EAD

The above table is what I look to be displayed in the php page rather than totally reading the lines and displaying the whole file. 上面的表格是我在php页面中显示的内容,而不是完全读取行并显示整个文件。

How can I change my simple code to get this? 如何更改我的简单代码才能获得此代码?

With that input format, this should produce the output you asked for: 使用该输入格式,这应该产生您要求的输出:

<?php
$file = fopen("somefile.txt",'r');
$class = 0;
while (!feof($file))
{
    $line = trim(fgets($file));
    if ($line)
    {
        if (strlen($line)==8 && substr($line, 0, 5)=="Class") $class = $line[6];
        elseif (is_numeric($line[0]))
        {
            $parts = explode(" ",$line);
            echo $class." | ".$parts[1]." | ".$parts[count($parts)-2]."<br>";
        }
    }
}
fclose($file);
?>

Well here is what you can do.. Its my own creation 那么这就是你能做的......它是我自己创造的

function mydata($file_path,$start_line=0,$end_line=0)
{
$urldatafile = file($file_path) or die("Sorry, Couldn't load data!!");
if($end_line==0){
    $linecount = count($urldatafile);
    $end_line = $linecount;
}
$array_data = array_slice($urldatafile, $start_line, $end_line);
    echo "<table>";
    foreach ($array_data as $data)
    { 
    $data = explode("===", $data);

    echo "<tr>";
        echo "<td>{$data[0]}</td><td>{$data[1]}</td><td>{$data[2]}</td><td>{$data[3]}</td>";
    echo "</tr>"
    }
    echo "</table>";
}

In example.txt file write each new line with the following data values 在example.txt文件中,使用以下数据值写入每个新行

Data1===data2===data3===data4
Data1===data2===data3===data4
Data1===data2===data3===data4
Data1===data2===data3===data4
Data1===data2===data3===data4

USAGE: 用法:

mydata("PATH TO EXAMPLE TEXT FILE");

Please note === it separates the two values in one line, also note that in text file new line should be new line not word wrapping.. 请注意===它将一行中的两个值分开,还要注意在文本文件中新行应该是新行而不是自动换行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM