在PHP中将文本文件过滤到HTML表格中

时间:2016-08-18 09:55:01

标签: php html file text

大家好我拥有 txt文件,内容如下:

    Name        : MyName1
    State       : Apt
    PathName    : C:\xx\MyName1.txt

    Name        : MyName2
    State       : Apf
    PathName    : C:\xx\MyName2.txt

    Name        : MyName3
    State       : Apf
    PathName    : C:\xx\MyName3.txt

有没有办法将此文本文件转换为 HTML TABLE ,使用PHP代码看起来像这样:

        Name        State   PathName
        MyName1     Apt     C:\xx\MyName1.txt
        MyName2     Apf     C:\xx\MyName2.txt
        MyName3     Apf     C:\xx\MyName3.txt

3 个答案:

答案 0 :(得分:2)

您可以尝试混合file_get_contents()foreach()& preg_replace()就像下面评论的代码所示:

TXT文件的内容:

    Name        : MyName1
    State       : Apt
    PathName    : C:\xx\MyName1.txt

    Name        : MyName2
    State       : Apf
    PathName    : C:\xx\MyName2.txt

    Name        : MyName3
    State       : Apf
    PathName    : C:\xx\MyName3.txt

表格创建算法:

    <?php

        $txtFile        = __DIR__ . "/test.txt"; //<== PATH TO THE TXT FILE
        $txtFileData    = file_get_contents($txtFile);

        // CONVERT $txtFileData TO ARRAY BY SPLITTING AT THE NEW-LINE BOUNDARY.
        $arrTxtData     = explode("\n", $txtFileData);
        $sections       = array();
        $index          = 0;

        // LOOP THROUGH THE ARRAY (FROM ABOVE) AND BUILD SECTION GROUPS
        // USING THE EMPTY LINE AS THE CUE-POINT FOR THE NEXT SECTION GROUP
        foreach($arrTxtData as $line){
            if(!empty(trim($line))){
                $sections[$index][] = trim($line);
            }else{
                $index++;
            }
        }

        // EXTRACT THE VALUES FOR THE TABLE HEADINGS...
        $extract    = $sections[0];
        $heading1   = preg_replace("#(\t|\s)*?(\:.*)$#", "", $extract[0]);
        $heading2   = preg_replace("#(\t|\s)*?(\:.*)$#", "", $extract[1]);
        $heading3   = preg_replace("#(\t|\s)*?(\:.*)$#", "", $extract[2]);

        // BUILD THE HEAD-SECTION OF YOUR TABLE...
        $output     = "<table class=''>" . PHP_EOL;
        $output    .= "<tr class=''>" . PHP_EOL;
        $output    .= "<th class=''>{$heading1}</th>" . PHP_EOL;
        $output    .= "<th class=''>{$heading2}</th>" . PHP_EOL;
        $output    .= "<th class=''>{$heading3}</th>" . PHP_EOL;
        $output    .= "</tr>" . PHP_EOL;
        $output    .= "<tbody>" . PHP_EOL;

        // LOOP THROUGH ALL THE SECTIONS AND BUILD YOUR ROWS + CELLS
        foreach($sections as $section){
            $output.= "<tr>" . PHP_EOL;
            foreach($section as $data){
                $strVal = preg_replace("#(^.*?\:\s*?)#", "", $data);
                $output.= "<td class=''>{$strVal}</td>" . PHP_EOL;
            }
            $output.= "</tr>" . PHP_EOL;
        }
        // CLOSE OFF THE <TBODY> AND <TABLE> TAGS
        $output    .= "</tbody>" . PHP_EOL;
        $output    .= "</table>" . PHP_EOL;

        // ECHO THE RESULTING OUTPUT [THE TABLE]
        echo $output;

ECHO声明的结果:

        Name        State   PathName
        MyName1     Apt     C:\xx\MyName1.txt
        MyName2     Apf     C:\xx\MyName2.txt
        MyName3     Apf     C:\xx\MyName3.txt

答案 1 :(得分:0)

// Read the contents of the file
$file = file_get_contents("someFile.txt");

// convert the file contents to lines of text
$lines = explode("\r\n", $ file);

现在您可以访问$ lines数组中的任意数据行。

答案 2 :(得分:0)

如果您不知道如何从此开始, 使用php explode将长字符串与单行中的所有信息分开 喜欢:

$array = explode (" : ", $string);

这会将:符号周围的每一行分开, 这样你有一个像这样的数组:

$array = [
    "Name",
    "Mynam1",
    "State",
    "Apt",
    "..."
];

然后你可以使用for循环遍历每个项目并将其存储为键和值对,这意味着每次rray项目都有一个偶数键值:0,2,4等...它将是您想要的键的名称(NAME,STATE,...),您可以将它与结果数组一起配对,它的值将是下一个项目(所以$x + 1)。

for($x = 0, $x < count($array); $x++) {
    if ( $x % 2 == 0 ) {
        $result_array["$array[$x]_$x"] = $array[$x+1];
    }
}

使用此方法,您将获得如下数组:

$result = [
    "Name_1" => "Mynam1",
    "State_1" => "Apt",
    "..."
];

现在从那里开始你应该尝试按照你的需要工作,这样你就可以根据需要使用它了。

祝你好运!