Read from file TXT

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Vladinator
Forum Newbie
Posts: 12
Joined: Mon Feb 28, 2005 6:08 pm

Read from file TXT

Post by Vladinator »

What script can I use, if I want php to read from a small txt file.
Like, if the file looks like this:

Code: Select all

name=Hans
password=something
email=some@one.com
How can I make the php get the name, like, get the text after "name=", "password=" and "email="?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

untested

Code: Select all

preg_match_all('#^\s*(.*?)\s*=\s*(.*?)\s*$#m', file_get_contents($filename), $matches);

print_r($matches);
Vladinator
Forum Newbie
Posts: 12
Joined: Mon Feb 28, 2005 6:08 pm

Post by Vladinator »

Worked!
Now, I just get this in the HTML code:

Code: Select all

Array
(
    ї0] => Hans
    ї1] => something
    ї2] => some@one.com
)
What script can I now use, to get the array 0 (Hans), 1 (something) and 2 (some@one.com)?
Using this script to display that information:

Code: Select all

$filename = '\accounts\hans';
$fp = fopen($filename,'r');

preg_match_all('#^\s*(.*?)\s*=\s*(.*?)\s*$#m', file_get_contents($filename), $array); 
print_r($arrayї2]);
Vladinator
Forum Newbie
Posts: 12
Joined: Mon Feb 28, 2005 6:08 pm

Post by Vladinator »

Found it out, tnx for help! :D

Used:

Code: Select all

print_r($arrayї'2']ї'0']);
print_r($arrayї'2']ї'1']);
print_r($arrayї'2']ї'2']);
Vladinator
Forum Newbie
Posts: 12
Joined: Mon Feb 28, 2005 6:08 pm

Post by Vladinator »

Oh, last quest, how can i fix this?

Code: Select all

if($username=="$got_name" AND $password==print_r($arrayї2]ї0])) {
Aaa... tried to make it like this:

Code: Select all

$pass = print_r($arrayї2]ї0]);
if($username=="$got_name" AND $password==$pass) {
But that did not work, the $pass just echoes the text, it does not keep it like a variable... :S
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$pass = $arrayї2]ї0];
...:?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

the lazy solution was: http://www.php.net/parse_ini_file
Vladinator
Forum Newbie
Posts: 12
Joined: Mon Feb 28, 2005 6:08 pm

Post by Vladinator »

Fixed it, finnaly! :D
Tnx, and good night, 01:20!!!
Post Reply