1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

reading lines from text file and storing in php variable

Discussion in 'PHP' started by JEET, May 22, 2006.

  1. #1
    Hi,
    I have the following in a text file:
    Line1,
    Line 2,
    Line 3,

    I have to display all lines in <h1> Line text </h1> format.
    If I can get the lines in a variable "1 by one", I can do it but I am not able to get lines in a variable "1 by one" and then echo them.

    How do I read lines from this text file 1 by one and then store the line in a variable for further action?

    Thank you
    jeet
     
    JEET, May 22, 2006 IP
  2. Big 'G'

    Big 'G' Member

    Messages:
    89
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    48
    #2
    file($file)
    Will load the file into an array then just loop the array
     
    Big 'G', May 22, 2006 IP
  3. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #3
    Hello,
    Is there a way to substitute "character reading" in the code below with "line reading"?

    Thanks
    <?php
    $f=fopen("file.txt","r") or exit("Unable to open file!");
    while (!feof($f))
    {
    $x=fgetc($f);
    // The line above stores a character in $x.
    echo $x;
    }
    fclose($f);
    ?>

    Instead of reading a character, can I read a line?
    Thanks
    jeet
     
    JEET, May 22, 2006 IP
  4. rickbkis

    rickbkis Peon

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Try 'fgets'.


    Personally, I like to use file_get_contents and then split on "\n":

    $array = split("\n", file_get_contents('filename'));
    Then just loop through the array.

    rickb
     
    rickbkis, May 22, 2006 IP
  5. PinoyIto

    PinoyIto Notable Member

    Messages:
    5,863
    Likes Received:
    170
    Best Answers:
    0
    Trophy Points:
    260
    #5
    You may also try this
    
    $urls="http://www.yourdomain.com/filename.ext";
    $page = join("",file("$urls"));
    $kw = explode("\n", $page);
    
    for($i=0;$i<count($kw);$i++){
    echo $kw[$i];
    }
    
    Code (markup):
     
    PinoyIto, May 22, 2006 IP
  6. rickbkis

    rickbkis Peon

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Ah.. right..

    I meant 'explode', not 'split'.

    rickb.
     
    rickbkis, May 22, 2006 IP
  7. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #7
    Exactly what I was looking for!:D :)
    Thanks so much.
    1 question... What is this line doing?
    $page = join("",file("$urls"));
    Thank you very much for the code.
    best wishes
    jeet
     
    JEET, May 23, 2006 IP
  8. Young Twig

    Young Twig Peon

    Messages:
    27
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Converts the array to a string. Join is an alias for implode. It takes each piece of the array, and glues them together, using the first argument as "glue."
     
    Young Twig, May 23, 2006 IP
  9. Veselin Stoilov

    Veselin Stoilov Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    you may also try with file_get_contents()
     
    Veselin Stoilov, Jun 6, 2006 IP
  10. qastex

    qastex Peon

    Messages:
    116
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Thanks, that snippet works fine.
     
    qastex, Dec 14, 2009 IP
  11. yoes_san

    yoes_san Peon

    Messages:
    443
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Or you can also do something like this

    
    //open file
    $fp = @fopen ($some_file, "r");
    
    if ($fp) {
         //for each line in file
         while(!feof($fp)) {
              //push lines into array
              $this_line = fgets($fp);					
              array_push($some_array,$this_line);
         }
         //close file
         fclose($fp);
    }
    
    Code (markup):
     
    yoes_san, Dec 14, 2009 IP