Results 1 to 4 of 4

Thread: Parse txt file, line by line..

  1. #1

    Thread Starter
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Parse txt file, line by line..

    I have a txt file thats storing data, so each new line will look like,

    aaa||bbb||ccc
    sss||ttt||rrr


    etc..

    any idea the best way to split each line one by one on the "||" then display those entries row by row, something like,

    guestbook table,

    name email comment
    aaa bbb ccc
    sss ttt rrr

    Have been trying for ages, playing about with,

    PHP Code:
    $file fopen("guestbook.txt""r");
      while(!
    feof($file))
        {
            
    $wrfgets($file);
            
    $s=explode("||",$wr);
            for(
    $i=0;$i<sizeof($s);$i++){

    ..... 
    kinda things....

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Parse txt file, line by line..

    This will put the data into a jagged array:
    PHP Code:
    $guestbook_lines file('guestbook.txt');
    $guestbook = array();

    foreach (
    $guestbook_lines as $n => $line)
      
    $guestbook[$n] = explode('||'$line); 

  3. #3

    Thread Starter
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Question Re: Parse txt file, line by line..

    I have this in my txt file,

    test1||test2||test3
    test1||test2||test3
    test1||test2||test3

    So this put each line into an array,

    HTML Code:
    $guestbook_lines = file('guestbook.txt');
    you can then split each line on the || which all looks fine,
    $guestbook = array();
    foreach ($guestbook_lines as $n => $line){
    $guestbook[$n] = explode('||', $line);

    }

    but if i do this the return output is ArrayArrayArray

    for($i=0;$i<sizeof($guestbook);$i++){
    echo $guestbook[$i];
    }

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Parse txt file, line by line..

    Yes. If you want to recursively inspect the structure of a variable, use print_r or var_dump.

    PHP Code:
    var_dump($guestbook); 
    That will show you that it is a jagged array (as I said).

Posting Permissions

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



Click Here to Expand Forum to Full Width