How can I pick and show a specific line from a text file with PHP?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • giorgisp
    New Member
    • May 2010
    • 6

    How can I pick and show a specific line from a text file with PHP?

    I have a text file with URLs. It is something like this below:

    http://site1.com/whatever.php
    http://site2.com/whatever.php
    http://site3.com/whatever.php
    http://site4.com/whatever.php
    http://site5.com/whatever.php
    http://site6.com/whatever.php
    http://site7.com/whatever.php
    ....

    And I want to:
    -Pick the first URL and put it in a variable
    -Pick a random URL and put it in a variable
    -Display all the URLs in a page the one below of the other

    Any help on this will be greatly appreciated, thanks!
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    I won't give you the actual code, but I'll give you the functions and pointers needed.

    With text-files that have one item per-row, the best function to use is file(). It basically takes each line of a file and appends it to an array.

    If you know your way around arrays in PHP, you'll know that you can obtain the first element of the array by using the index 0.

    To grab a random(-ish) element of the array, take a look at the rand() function, paying attention to the $min and $max parameters. Set the $min parameter to 0 or 1, depending on whether you want the first element to be included. The $max parameter should be set to the size of your array (count()) minus 1. Remember, arrays in PHP start at 0. So, if count($my_arr); returns 10, the 10th element of that array is going to be at index 9 - $my_arr[9] or $my_arr[count($my_arr) - 1];

    To print all the elements of that array, simply foreach() through them.

    Hope this helps,
    Mark.

    Comment

    • giorgisp
      New Member
      • May 2010
      • 6

      #3
      Thank you very much!

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Originally posted by giorgisp
        Thank you very much!
        Sure thing.

        Welcome to Bytes.com :)

        Comment

        • giorgisp
          New Member
          • May 2010
          • 6

          #5
          cleaned the links

          Also, is there any way to write a url into the file but at the end? Example:

          http://site1.com/whatever.php
          http://site2.com/whatever.php
          http://site3.com/whatever.php

          and write a new url at the end:

          http://site1.com/whatever.php
          http://site2.com/whatever.php
          http://site3.com/whatever.php
          http://site4.com/whatever.php

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            fwrite() is used to write data to a file. check out the examples to see how it is used.

            Comment

            Working...