Array from file

Discussion in 'PHP' started by Voimis, Oct 1, 2007.

  1. #1
    Hi dudes!

    I need code which picks .txt rows from . txt file to array. So there should be array = { "first row", "second row"; } etc.

    Thanks a lot!

    Voimis
     
    Voimis, Oct 1, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    nico_swd, Oct 1, 2007 IP
  3. Voimis

    Voimis Peon

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    So... How to use it :D

    <?php

    $lines = file('http://www.example.com/example.txt');
    array = {$lines};

    ?>

    Is this the right way or or or... I´m just now on school so I can´t check how it works, just quessing right now.

    sorry, I´m just a noob!
     
    Voimis, Oct 1, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    In your example, $lines would be the array. file() does the whole job for you.
     
    nico_swd, Oct 1, 2007 IP
  5. nvm3media

    nvm3media Peon

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Just so you can see how it works...

    <?php

    $lines = file('yourfile');
    print_r($lines);

    ?>
     
    nvm3media, Oct 1, 2007 IP
  6. ziogas

    ziogas Peon

    Messages:
    32
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Or you can do like this to quickly access every array item and maybe modify it and print out

    $lines = file('http://www.example.com/example.txt');
    foreach($lines as $line)
    echo htmlspecialchar($line).'<br />';
     
    ziogas, Oct 1, 2007 IP