Aston Posted June 1, 2012 Share Posted June 1, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/263510-need-help-with-printing-links-with-vars/ Share on other sites More sharing options...
DavidAM Posted June 1, 2012 Share Posted June 1, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/263510-need-help-with-printing-links-with-vars/#findComment-1350435 Share on other sites More sharing options...
Aston Posted June 2, 2012 Author Share Posted June 2, 2012 add it right after $link = explode(',',$activelink); ? Quote Link to comment https://forums.phpfreaks.com/topic/263510-need-help-with-printing-links-with-vars/#findComment-1350439 Share on other sites More sharing options...
Aston Posted June 2, 2012 Author Share Posted June 2, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/263510-need-help-with-printing-links-with-vars/#findComment-1350448 Share on other sites More sharing options...
Aston Posted June 2, 2012 Author Share Posted June 2, 2012 NVM solved Quote Link to comment https://forums.phpfreaks.com/topic/263510-need-help-with-printing-links-with-vars/#findComment-1350452 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.