Reputation: 23
I want to enter each line of this text file in to a new array element, and i need the array to end up like so: Array ( [testuser] => 'testpassword' ) by using this text in the text file: 'testuser' => 'testpass'
The code i have now:
$filename = "logininfo.txt";
$fp = @fopen($filename, 'r');
if ($fp) { $LOGIN_INFORMATION = explode("\n", fread($fp, filesize($filename))); }
Upvotes: 2
Views: 560
Reputation: 265966
Use PHP's file
function:
$array = file('logininfo.txt');
To ignore newlines and empty lines, provide the appropriate flags:
$array = file('logininfo.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
A good way to solve this, is PHP's parse_ini_file
function.
Your file will have to look slightly different though, but I assume that is not a problem:
; file contents:
testuser = testpass
And in your PHP file:
$array = parse_ini_file('logininfo.txt', FALSE);
Upvotes: 4
Reputation: 1827
Assuming your file is like this :
user : blah password : somthing
you could do it like this
$arr = array();
$filename = "test.txt";
$fp = fopen( $filename, "r" );
while ( ! feof( $fp ) ) {
$line = fgets( $fp, 1024 );
$temp = explode(":", $line);
$arr[trim($temp[0])] = trim($temp[1]);
}
Upvotes: 0