how to get urls from a text file and make them hyperlinked

Discussion in 'PHP' started by smartperson, Dec 2, 2010.

  1. #1
    Hello,

    i have a text file that contain many urls each url in a line in which i want to import into php webpage and make each url hyperlinked, can anybody help me in doing that, does anybody know the php code to do such thing,

    thanks
    mohammed
    :eek:
     
    smartperson, Dec 2, 2010 IP
  2. mikecampbell

    mikecampbell Peon

    Messages:
    26
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Sure just use:

    
    $urls =file('path/to/urls.txt');
    foreach ($urls as $url) {
      echo "<a href=\"$url\">$url</a><br>";
    }
    
    PHP:
     
    mikecampbell, Dec 2, 2010 IP
  3. nadeem3366

    nadeem3366 Member

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #3
    Hi mikecampbell;

    Can you please define [ $urls =file('path/to/urls.txt'); ] that what will be "path" and what is "to" ?

    Thanks
     
    nadeem3366, Dec 3, 2010 IP
  4. mikecampbell

    mikecampbell Peon

    Messages:
    26
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The string path/to/urls.txt is just an example. Replace it with the real path to your file full of URLs.
     
    mikecampbell, Dec 4, 2010 IP
  5. manijan

    manijan Greenhorn

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #5
    php array is the best method to use this file. just use the following code which will print each your url line and will convert it into hyperlink

    $urls =file('your_file_location/to/file_name.txt');
    foreach ($urls as $url) {
    echo "<a href=\"$url\">$url</a><br>";
    }
     
    manijan, Dec 4, 2010 IP
  6. Nahid

    Nahid Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You can use this one too. This will eliminate unnecessary line break and elements.

    <?php
    $urls=explode("\n",trim(file_get_contents('path/to/urls.txt')));
    foreach ($urls as $url) {
    echo "<a href=\"$url\">$url</a><br>";
    }
    ?>
    Code (php):
     
    Last edited: Dec 6, 2010
    Nahid, Dec 6, 2010 IP