I have a file, which contains passwords/usernames:
thomas 123
john 321What would be the best way to identify lines and add to them contact information? Right in password file I dont want store, because the size of the file matters.. I thought to add an identifier string before records, like this:
[1]
thomas 123 [2] john 321What do you think? It is a good way to identify lines? If yes, how I can read out line after identifier [number]?
开发者_如何学编程If I can read out, then in a second file I can store contact information.
[1]
email, etc [2] email etcthis is a config file of an offline app, sadly I cant change the structure of that file (max. change: add an identifier before lines) or use database.
Thank you so much,
Adrian
You could use a while loop that reads every other line as your id and every other line as your username and password; however, like what @zerkms said, you're better off putting your energy into a tried and tested database.
Learning to use a database will save you time in the future so that on each project you don't have to figure out exactly how to manipulate a flat file.
Reusable solutions enable and empower you to focus on your goal and your product, whatever that may be.
As zerkms wrote in his comment to your question, you should probably use a database for that. If you don't feel comfortable with setting up a database on a server, look into SQLite (php manual) which is a file-based database which is really easy to use and setup.
If you want to stick with the text-file approach, why write your own format and parser? You could just as well use an existing format like JSON (can be parsed with json_decode) or even an INI file (use parse_ini_file).
And: Never store your text-file at a place where it's globally accessible (eg. webroot).
Supposedly the usernames in the password file are unique (otherwise it wouldn't be very useful). Assuming that's the case, why can't you leave its structure as is without adding anything to it? Then your contact file could be formatted as an INI file or JSON file as bummzack suggested with the username as the key.
You can handle this as simple as a single call to file function.
$lines = file ('passwords.txt');
After that you'll have all lines numbered from 0 to N-1.
精彩评论