PunjabHaker Posted December 31, 2006 Share Posted December 31, 2006 [b]passwords.txt contain:[/b]user1 pass1user2 pass2[b]sites.txt contain:[/b]site1site2[b]Code:[/b]<?php$handle = fopen("passwords.txt", "r");$lines = file('sites.txt');foreach ($lines as $line_num => $site) {while ($userinfo = fscanf($handle, "%s\t%s")) {list ($user, $pass) = $userinfo;print "$site : $user : $pass \n";}}?>[b]I need it to show me like that:[/b]site1 : user1 : pass1site1 : user2 : pass2site2 : user1 : pass1site2 : user2 : pass2[b]Question:[/b] Where i'm wrong ? Link to comment https://forums.phpfreaks.com/topic/32413-solved-read-multiple-files/ Share on other sites More sharing options...
Barand Posted December 31, 2006 Share Posted December 31, 2006 Having got to the end of file for passwords.txt when you process site1 you need to "rewind()" it for site2[code]<?php$handle = fopen("passwords.txt", "r");$lines = file('sites.txt');foreach ($lines as $site) { while ($userinfo = fscanf($handle, "%s %s")) { list ($user, $pass) = $userinfo; print "$site : $user : $pass <br>\n"; } rewind($handle);}fclose ($handle);?>[/code] Link to comment https://forums.phpfreaks.com/topic/32413-solved-read-multiple-files/#findComment-150558 Share on other sites More sharing options...
PunjabHaker Posted January 1, 2007 Author Share Posted January 1, 2007 [b]It prints me like that:[/b]site1 : user1 : pass1site1 : user2 : pass2site2 : user1 : pass1site2 : user2 : pass2[b]Question:[/b] How can i make it to print like site2 (not like site1) ? Link to comment https://forums.phpfreaks.com/topic/32413-solved-read-multiple-files/#findComment-150584 Share on other sites More sharing options...
Barand Posted January 1, 2007 Share Posted January 1, 2007 When you use file() the linefeeds are retained. Use trim() to remove[code]<?php$handle = fopen("passwords.txt", "r");$lines = file('sites.txt');foreach ($lines as $site) { while ($userinfo = fscanf($handle, "%s %s")) { $site = trim($site); // <-- add this line list ($user, $pass) = $userinfo; print "$site : $user : $pass <br>\n"; } rewind($handle);}fclose ($handle);?>[/code] Link to comment https://forums.phpfreaks.com/topic/32413-solved-read-multiple-files/#findComment-150736 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.