php file reading

Discussion in 'PHP' started by arunsuriindia, Mar 12, 2007.

  1. #1
    i have a csv file which i want to read via php in an array

    i think the command is

    $f=file($filename)


    now i want to edit this $f array calculate the number of column

    access it as

    $f[row][column]

    how do i do all this
     
    arunsuriindia, Mar 12, 2007 IP
  2. Robert Plank

    Robert Plank Peon

    Messages:
    55
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Example:

    <?php
    
    $filename = "csvDemo.txt";
    
    // Read the file
    $list = array_map("trim", file($filename));
    
    // Split columns
    $list = array_map(
       create_function('$line', 'return split(",", $line);'),
       $list
    );
    
    // Which column and row do I want to look at?
    $column = 3;
    $row = 2;
    
    echo $list[$column-1][$row-1];
    
    ?>
    PHP:
    csvDemo.txt contains:

    one,two,three
    four,five,six
    seven,eight,nine
    Code (markup):
     
    Robert Plank, Mar 12, 2007 IP
  3. arunsuriindia

    arunsuriindia Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    that was great

    thanx

    do you know how read a table in html file into mysql
     
    arunsuriindia, Mar 12, 2007 IP
  4. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #4
    When you say table do you mean

    <table><tr><td></td></tr></table>

    or are you still talking about the CSV table?
     
    Weirfire, Mar 12, 2007 IP
  5. Robert Plank

    Robert Plank Peon

    Messages:
    55
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    <?php
    
    $filename = "tableDemo.html";
    
    $contents = file_get_contents($filename);
    
    function matchTag($tagName, $contents) {
       if (preg_match_all('|<' . preg_quote($tagName) . '[^>]*>(.*?)</' . preg_quote($tagName) . '[^>]*>|si', $contents, $matches)) {
          return $matches[1];
       }
    }
    
    $grid = array();
    
    // Match a table
    if ($tables = matchTag("table", $contents)) {
       // Match only the first table
       $table = reset($tables);
    
       // Match rows
       if ($rows = matchTag("tr", $table)) {
          foreach ($rows as $rowNumber => $row) {
    
             // Match columns
             if ($cols = matchTag("td", $row)) {
                $grid[$rowNumber] = $cols;
             }
          }
       }
    }
    
    echo "<xmp>";
    print_r($grid);
    echo "</xmp>";
    
    ?>
    PHP:
     
    Robert Plank, Mar 12, 2007 IP
  6. arunsuriindia

    arunsuriindia Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    thats great of you
    got an idea but still couldn`t get the content spot right.
     
    arunsuriindia, Mar 12, 2007 IP
  7. jitesh

    jitesh Peon

    Messages:
    81
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    <?php
    $row = 1;
    $handle = fopen("test.csv", "r");
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count($data);
    echo "<p> $num fields in line $row: <br /></p>\n";
    $row++;
    for ($c=0; $c < $num; $c++) {
    echo $data[$c] . "<br />\n";
    }
    }
    fclose($handle);
    ?>
     
    jitesh, Mar 13, 2007 IP