|
-
Sep 14th, 2005, 11:25 PM
#1
Thread Starter
Lively Member
[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:
<?php
function ConvertToLinks ($vert)
{
$vert = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)','\\1<a href="http://\\2">\\2</a>', $vert);
return $vert;
}
#***************************************
#Program start
#
#
#***************************************
$filename = "text.txt";
$flepointer = fopen ($filename, "r") or die ("Could not open the file");
$contents = fread ($flepointer, filesize ($filename));
fclose($flepointer);
echo ConvertToLinks($contents);
?>
-
Sep 15th, 2005, 01:11 AM
#2
Re: Create hyperlinks from text file
I have copied your code and it works for me. What part are you getting an error with?
-
Sep 15th, 2005, 10:26 PM
#3
Thread Starter
Lively Member
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
-
Sep 19th, 2005, 05:53 PM
#4
Thread Starter
Lively Member
Re: Create hyperlinks from text file
-
Sep 20th, 2005, 03:19 AM
#5
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;
}
-
Sep 20th, 2005, 05:55 PM
#6
Thread Starter
Lively Member
Re: Create hyperlinks from text file
Thanks for the help
Your code does work but returns error. I will try to fix the errors.
-
Sep 20th, 2005, 10:11 PM
#7
Re: Create hyperlinks from text file
What's the error? It worked fine for me.
-
Sep 20th, 2005, 10:43 PM
#8
Thread Starter
Lively Member
Re: Create hyperlinks from text file
Made some changes and got it to work. Thanks for the help
VB Code:
<?php
function ConvertToLinks ($vert)
{
$x=0;
$y=$x+1;
$textarrary = explode ("\n", $vert);
while($x < count($textarrary))
{
echo "<a href=\"http://$textarrary[$y]\">$textarrary[$x]</a><br>";
$x=$x+2;
$y=$x+1;
}
return $vert;
}
#***************************************
#Program start
#
#
#***************************************
$filename = "text.txt";
$flepointer = fopen ($filename, "r") or die ("Could not open the file");
$contents = fread ($flepointer, filesize ($filename));
fclose($flepointer);
ConvertToLinks($contents);
?>
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|