Results 1 to 8 of 8

Thread: [RESOLVED] Create hyperlinks from text file

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2002
    Posts
    68

    Resolved [RESOLVED] Create hyperlinks from text file

    I am trying to make a script that reads urls from a text file.
    The first line of the text file will contain the text that should be displayed on the webpage. The second line of the file will contain the actual link to the website. I have include the script so far. Thanks for the help.


    Text File

    First web page
    www.firstpage.com
    Second page
    www.secondlink.com

    VB Code:
    1. <?php
    2.  
    3. function ConvertToLinks ($vert)
    4.  
    5. {
    6.  
    7. $vert = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)','\\1<a href="http://\\2">\\2</a>', $vert);           
    8.                      
    9.                      
    10. return $vert;
    11. }
    12.  
    13.  
    14.  
    15.  
    16. #***************************************
    17. #Program start
    18. #
    19. #
    20. #***************************************
    21.  
    22. $filename = "text.txt";
    23. $flepointer = fopen ($filename, "r") or die ("Could not open the file");
    24. $contents = fread ($flepointer, filesize ($filename));
    25. fclose($flepointer);
    26. echo ConvertToLinks($contents);
    27.  
    28.  
    29.  
    30. ?>

  2. #2
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: Create hyperlinks from text file

    I have copied your code and it works for me. What part are you getting an error with?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2002
    Posts
    68

    Re: Create hyperlinks from text file

    Thanks for the reply
    You are correct the code does work but want I am trying to do is make the firstline in the textfile the actual link and the second line is the website the link is pointing to. See the example below.


    Text file
    --------------
    I read from the file
    www.cnet.com
    the second line
    www.yahoo.com


    Desired output
    ----------------
    I read from the file
    the second line

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2002
    Posts
    68

    Re: Create hyperlinks from text file

    Any ideas.

    Thanks

  5. #5
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: Create hyperlinks from text file

    This may not be the best way to do it but it works. HTH

    PHP Code:
    $fc=file("text.txt");
    $i "0";
    $s"1";
    $URLArray $fc//Make array out of text file

    //loop through array using foreach
    foreach($fc as $line) {
    echo 
    "<a href=\"http://$URLArray[$s]\">$URLArray[$i]</a><br>";

    $i $i +2;
    $s $s +2;


  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2002
    Posts
    68

    Re: Create hyperlinks from text file

    Thanks for the help

    Your code does work but returns error. I will try to fix the errors.

  7. #7
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: Create hyperlinks from text file

    What's the error? It worked fine for me.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Mar 2002
    Posts
    68

    Re: Create hyperlinks from text file

    Made some changes and got it to work. Thanks for the help

    VB Code:
    1. <?php
    2. function ConvertToLinks ($vert)
    3.  
    4. {
    5. $x=0;
    6. $y=$x+1;
    7.  
    8. $textarrary = explode ("\n", $vert);
    9.  
    10. while($x < count($textarrary))
    11.  
    12.     {
    13.     echo "<a href=\"http://$textarrary[$y]\">$textarrary[$x]</a><br>";
    14.         $x=$x+2;
    15.                           $y=$x+1;
    16.     }      
    17.      
    18.      
    19.          return $vert;
    20. }
    21.    
    22.  
    23.  
    24. #***************************************
    25. #Program start
    26. #
    27. #
    28. #***************************************
    29.  
    30. $filename = "text.txt";
    31. $flepointer = fopen ($filename, "r") or die ("Could not open the file");
    32. $contents = fread ($flepointer, filesize ($filename));
    33. fclose($flepointer);
    34. ConvertToLinks($contents);
    35. ?>
    Last edited by gearbolt; Sep 21st, 2005 at 08:57 AM.

Posting Permissions

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



Click Here to Expand Forum to Full Width