Jump to content

need help with printing <a href> links with vars


Aston

Recommended Posts

Hi guys i have a .txt file with links in it looks like this:

Facebook,www.facebook.com

Gmail,mail.google.com

 

im trying to print them  as a href links, it almost works.

 

here is my code:

function printlinks()
{
$filename ='C:\wamp\www\fana\Files\links.txt';
$handle = fopen($filename, 'r');
$contents = fread($handle, filesize($filename));
fclose($handle);

$links = explode("\n",$contents);
foreach($links as $activelink)
{	
	$link = explode(',',$activelink);
	echo '<a href="'.$link[1].'">'.$link[0].'</a>'.'<br />';
}
}

and then i just use printlinks();

 

explode works well and it shows the links like i wanted but i still get an error:

Notice: Undefined offset: 1 in C:\wamp\www\fana\managers\manager.php on line 24
Call Stack
#	Time	Memory	Function	Location
1	0.0004	673392	{main}( )	..\index.php:0
2	0.0014	692320	printlinks( )	..\index.php:17

 

This is line 24:

echo '<a href="'.$link[1].'">'.$link[0].'</a>'.'<br />';

 

Really need help with it, thanks alot :)

 

Link to comment
Share on other sites

If $link[1] does not exist. Then explode did not find a comma in the string.

 

You might add a check after the explode:

if (count($link) < 2) {
  trigger_error('invalid line in link file: ' . $activelink, E_USER_WARNING);
}

 

that should show you the bad line so you can go fix it in the text file.

Link to comment
Share on other sites

Guys i think i know what it is and need help with solving it.

when i open my txt file my pointer is always at a beginning of a new empty line, when i backspace and bring my pointer to the last line with a text it works well.

 

how do i solve it?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.