omdah omdah - 3 months ago 27
HTML Question

Filter Text File into HTML Table in PHP

Hi everyone i have txt file with the following contents:

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


Is there any way to convert this text file into HTML TABLE by using PHP code to look something like this:

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

Answer

You could try a blend of file_get_contents(), foreach() & preg_replace() for that like the commented code below illustrates:

CONTENTS OF TXT FILE:

    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

TABLE CREATION ALGORITHM:

    <?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;

RESULT OF THE ECHO STATEMENT:

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