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 07-16-2008, 06:16 PM   #1 (permalink)
The Acquainted
 
Peuplarchie's Avatar
 
Join Date: May 2008
Location: Québec
Posts: 104
Thanks: 10
Peuplarchie is on a distinguished road
Application Make array with txt file content

Good day to you all,
Here is a piece of code from my login script.

PHP Code:

$data
=array("user1"=>array("url"=>"Index/admin-index.php","password"=>"pass1"),
            
"user2"=>array("url"=>"file2.php","password"=>"pass2")); 
As you can see the array is not a generated array, in other word, the script should read a txt file, each line would have, a password, username and a url.
like :
Jack=>Daniel=>Director/index.php
Angolina=>Joli=>Pretty/index.php
Mike=>Lee=>User/index.php

Thanks !
Franck
__________________
That's why we are not alone on earth... let's build !
Peuplarchie is offline  
Reply With Quote
Old 07-16-2008, 08:28 PM   #2 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

You could use fscanf...
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 07-17-2008, 06:27 PM   #3 (permalink)
The Contributor
 
Ross's Avatar
 
Join Date: Jan 2008
Location: England, UK
Posts: 83
Thanks: 3
Ross is on a distinguished road
Default

I would use explode to do this, although fscanf looks a lot easier - I've never seen it before and I'd recommend it now!

PHP Code:
<?php

/**
 * Explode method
 */

// First explode the file into seperate lines and loop through to remove empty lines, etc
$file file_get_contents('file.txt');
$file explode("\n"$file);

$lines = array();

foreach(
$file as $line)
{
     
// Checks a line is not empty (after whitespace is removed) or not a comment (using .ini style comments)
     
if(!empty(trim($line)) && substr($line01) != ';')
     {
          
$lines[] = trim($line);
     }
}

// Next parse the data from the line and add to the $data array
$data = array();
foreach(
$lines as $line)
{
     
$line explode('=>'$line);
     
// Check the line is an array and hasn't done something silly
     
if(is_array($line))
     {
          
$data[] = $line;
     }
}

print_r($data);


/**
 * fscanf method
 */
$file fopen('file.txt''r');

$data_array = array();

while(
$data fscanf($file'%s=>%s=>%s'))
{
     list(
$name1$name2$file) = $data;
     
     
// Example
     
$data_array[$name1] = array($name2$file);
}

fclose($file);

print_r($data_array);

?>
As you can see the explosion method is much more memory intensive but shows you how a file could be parsed. fscanf is much more simple to use.
Ross 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 04:13 AM.

 
     

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