TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-03-2007, 05:44 AM   #1 (permalink)
The Visitor
Newcomer 
 
Join Date: Dec 2007
Posts: 1
Thanks: 0
Nathan is on a distinguished road
Default 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
Nathan is offline  
Reply With Quote
Old 12-03-2007, 05:56 AM   #2 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

I have the solution.

I will post in half an hour since I need to go.
Haris is offline  
Reply With Quote
Old 12-03-2007, 07:31 AM   #3 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

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];
}

?>
Haris is offline  
Reply With Quote
Old 12-03-2007, 09:17 AM   #4 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

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..
Tanax is offline  
Reply With Quote
Old 12-03-2007, 10:40 AM   #5 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

Quote:
Originally Posted by Tanax View Post
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.
Haris is offline  
Reply With Quote
Old 12-03-2007, 02:19 PM   #6 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Yea, but it could be a nice way to assign userid's ;)
Ofcourse it's optional :)
Tanax is offline  
Reply With Quote
Old 12-03-2007, 02:28 PM   #7 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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!
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-03-2007, 06:16 PM   #8 (permalink)
The Acquainted
 
wGEric's Avatar
 
Join Date: Nov 2007
Posts: 166
Thanks: 0
wGEric is on a distinguished road
Default

Use file to load the usernames and passwords in the text file into an array. Everyone is skipping that step.
__________________
Eric
wGEric is offline  
Reply With Quote
Old 12-03-2007, 06:32 PM   #9 (permalink)
The Wanderer
 
Join Date: Nov 2007
Posts: 12
Thanks: 0
DarkPrince11 is on a distinguished road
Default

Quote:
Originally Posted by wGEric View Post
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.
Send a message via AIM to DarkPrince11 Send a message via MSN to DarkPrince11
DarkPrince11 is offline  
Reply With Quote
Old 12-03-2007, 07:07 PM   #10 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

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.
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Karl is offline  
Reply With Quote
Old 12-03-2007, 08:20 PM   #11 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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. :)
Salathe is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 05:16 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design