Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    May 2006
    Posts
    216
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question How to read lines from TXT file?

    I like to put tag clouds into my script like:
    PHP Code:
    PHP Code:


            
    <ul>
                    <
    li class="tag1"><a href="#">Lorem ipsum</a></li>
            </
    ul
    How to read with PHP from defined path and its structure?
    I enclose my TXT file with name tag terms.
    Attached Files Attached Files
    Last edited by toplisek; 12-07-2010 at 01:40 PM.

  • #2
    New Coder
    Join Date
    Dec 2010
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Use 'fgets' function to read text file line by line. Below is an example.

    <?php
    $handle = @fopen("/tmp/inputfile.txt", "r");
    if ($handle) {
    while (!feof($handle)) {
    $buffer = fgets($handle, 4096);
    echo $buffer;
    }
    fclose($handle);
    }
    ?>

  • #3
    Regular Coder
    Join Date
    May 2006
    Posts
    216
    Thanks
    4
    Thanked 0 Times in 0 Posts
    You see structure? Is there mistake?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Late, but spam lead me here.
    Look into using fgetcsv to do this easily: http://php.ca/manual/en/function.fgetcsv.php. I don't know what all of your fields are for, but use a \t for your delimiter to chunk an array out of it. Other options include using things like *scanf family to separate it into chunks, or the strtok to tokenize it and suck the variables from the resulting strings.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

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