[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);
?>
Re: Create hyperlinks from text file
I have copied your code and it works for me. What part are you getting an error with?
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
Re: Create hyperlinks from text file
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;
}
Re: Create hyperlinks from text file
Thanks for the help
Your code does work but returns error. I will try to fix the errors.
Re: Create hyperlinks from text file
What's the error? It worked fine for me.
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);
?>