SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Interpret text file and convert to HTML table

    Hi everyone,

    I have a text file in the following format:

    Code:
    Text[tab]Text[tab]Text
    Text[tab]Text[tab]Text
    Text[tab]Text[tab]Text
    I want a PHP function that will take this file and convert it into an HTML table. At present, each "Text" field needs to be able to contain alphanumeric characters, spaces, commas, brackets and hyphens.

    Does anyone have a regex that would do this?

    Many thanks

    Sam
    Sam Hastings

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Regex? No need.
    This is what I'd do:
    PHP Code:
    <?
    $fulltext 
    file_get_contents("text.txt");
    $linearray explode("\n"$fulltext);
    foreach(
    $linearray as $line => $text){
        
    $fullarray[] = explode("\t"$text);
    }
    echo 
    "<table>";
    foreach(
    $fullarray as $key => $value){
    echo 
    "<tr>";
    foreach(
    $value as $which => $text){
    echo 
    "<td>{$text}</td>";
    }
    echo 
    "</tr>;
    }
    echo "
    </table>";
    ?>
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Sorry, forgot an ending double-quote.
    PHP Code:
    <?
    $fulltext 
    file_get_contents("text.txt");
    $linearray explode("\n"$fulltext);
    foreach(
    $linearray as $line => $text){
        
    $fullarray[] = explode("\t"$text);
    }
    echo 
    "<table>";
    foreach(
    $fullarray as $key => $value){
        echo 
    "<tr>";
        foreach(
    $value as $which => $text){
            echo 
    "<td>{$text}</td>";
        }
        echo 
    "</tr>";
    }
    echo 
    "</table>";
    ?>
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, that works perfectly

    Sam
    Sam Hastings


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •