Page 1 of 1

Check line starting with...

Posted: Thu Mar 04, 2004 7:00 am
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

Posted: Thu Mar 04, 2004 8:20 am
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!";
    }

}

Posted: Thu Mar 04, 2004 9:27 am
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.

Posted: Thu Mar 04, 2004 9:55 am
by davy2001
thx so much

that worked great