TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Text File Into Array (http://www.talkphp.com/absolute-beginners/1576-text-file-into-array.html)

Nathan 12-03-2007 05:44 AM

Text File Into Array
 
I have text file which goes like

username1|pass1
username2|pass2

I want to split it so all of the usernames are in one array and all the passwords are in one array. How can I do this?

Thanks,
Nathan

Haris 12-03-2007 05:56 AM

I have the solution.

I will post in half an hour since I need to go.

Haris 12-03-2007 07:31 AM

php Code:
<?php

// Array of username and password
// Array ( [0] => username1|pass1 [1] => username2|pass2 )
$szLines[] = "username1|pass1";
$szLines[] = "username2|pass2";

foreach($szLines as $iLine => $szLine)
{
    // Explode the array in terms of the bar | into an associative array
    // Array ( [0] => Array ( [0] => username1 [1] => pass1 ) [1] => Array ( [0] => username2 [1] => pass2 ) )
    $aExplode[] = explode('|', $szLine);
   
    // Feed the usernames to $aUsername;
    $aUsername[] = $aExplode[$iLine][0];
    // Feed the passwords to $aPassword;
    $aPassword[] = $aExplode[$iLine][1];
}

?>

Tanax 12-03-2007 09:17 AM

You could also make the loop so it puts an index number for each user.

PHP Code:

$aUsername[$i][] = $aExplode[$iLine][0]; 

That way you can make some nice member listing functions, or memberscount, etc..

Haris 12-03-2007 10:40 AM

Quote:

Originally Posted by Tanax (Post 5095)
You could also make the loop so it puts an index number for each user.

PHP Code:

$aUsername[$i][] = $aExplode[$iLine][0]; 

That way you can make some nice member listing functions, or memberscount, etc..

With or without $i, it'll still help with making member listing or members count functions. :-!

It's overkill.

Tanax 12-03-2007 02:19 PM

Yea, but it could be a nice way to assign userid's ;)
Ofcourse it's optional :)

Wildhoney 12-03-2007 02:28 PM

May I answer now? Here's my solution:

php Code:
$aUsername = array();
$aPassword = array();

foreach(explode("\n", $szText) as $szLine)
{
    list($aUsername[], $aPassword[]) = explode('|', $szLine);
}

You will then have an array of user names in $aUsername, and their respective passwords in $aPassword.

$szText will be your string:

Quote:

username1|pass1
username2|pass2
Hope this helps you!

wGEric 12-03-2007 06:16 PM

Use file to load the usernames and passwords in the text file into an array. Everyone is skipping that step.

DarkPrince11 12-03-2007 06:32 PM

Quote:

Originally Posted by wGEric (Post 5157)
Use file to load the usernames and passwords in the text file into an array. Everyone is skipping that step.

lol I think that's because it's a pretty obvious step in the procecss. ;-)

Karl 12-03-2007 07:07 PM

I just want to point out that it can be a very bad idea to use file. Depending on the size of the file contents you could be better off parsing the file in chunks using a temporary buffer, this is especially true when dealing with large files.

Salathe 12-03-2007 08:20 PM

Just to offer up another approach (simply to show there are always other options available) here's one using the lovely function: fgetcsv

PHP Code:

<?php

$aUsername 
= array();
$aPassword = array();
$szDbFile  'flatfile.txt';

if (
is_readable($szDbFile))
{
    
$pDb fopen($szDbFile'r');
    while (
false !== ($aDbRow fgetcsv($pDb100'|')))
    {
        if (isset(
$aDbRow[1]))
        {
            
$aUsername[] = $aDbRow[0];
            
$aPassword[] = $aDbRow[1];
        }
    }
    
fclose($pDb);
}


header('Content-Type: text/plain');
var_dump($aUsername$aPassword);

There are drawbacks (and advantages) to using this approach, I'll leave discovering those as an exercise for the reader. :)


All times are GMT. The time now is 01:01 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0