Check line starting with...

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
davy2001
Forum Commoner
Posts: 37
Joined: Tue Feb 24, 2004 5:59 pm

Check line starting with...

Post by davy2001 »

i have a text file with multiple lines

when a user registers on my site, i want it to say if the username is in use or not

so, i was wondering if there was a piece of script that checks the start of A line in the text file (if a line in the text file starts with "whatever" then echo username already in use)

in the text file it says:
userpass = bobbob (Username: bob, Password:bob)

Dave
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Post by liljester »

it would probably be easier to delimit your textfile with a comma or a tab... then it would be a matter of exploding one line at a time and checking to see if the username is there

if your txt file looks like this:
bob,bob
fred,password
bill,drowssap

you could read it like this:

Code: Select all

$txt_file = fopen("file.txt", "rb");
while($file_line = fgets($txt_file)){
    $line_array = explode(",", $file_line);
    $username = $line_array[0];
    
    if($register_name == $username){
        print"That name is taken!";
    }

}
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Indeed. My classic way of organizing data within text files is:

-Each item seperated by //
-Each line seperated by \r\n (windows) or \n (*nix)

I dislike anything besides the double slashes because how often is it used within data compared to commas, or etc. But everyone has their own favorite, this just happens to be mine.
davy2001
Forum Commoner
Posts: 37
Joined: Tue Feb 24, 2004 5:59 pm

Post by davy2001 »

thx so much

that worked great
Post Reply