/    Sign up×
Community /Pin to ProfileBookmark

Getting values from a .txt file..

This is a tricky one, I have a .txt file which has about 2,000 registers with employees names, ids, address, date of birth and etc.

I need to read this file and get only the employees ID numbers and their names.

I found out how to find a specific value on the .txt file, for example “ID” but I need to get what comes after it, which is the actual ID number, and do the same for the name,, so at the end I have an array with all the employess names and ID numbers.

This is how I´m doing:

[code=php]<?

$key = “ID”;
$key2 = “name”;

//load file into $fc array
$fc = file(“Employees.txt”);

foreach($fc as $line_num => $line){
echo “Line:<b>{$line_num}</b> : ” . $line . “<br />n”;

if (strstr($line,$key)){
echo “———————Found the ID:” .$key. “————————-<br>”;
}
}

fclose($fc);

?>[/code]

The way it is, I only find out whenever there is a “ID” string on the .txt when I need what comes after it, so I can put it into an array. Anyone ever had to do something like this?? Tks

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 03.2006 — If the lines in the text file are uniformly formatted, you could use substr() to extract the text of interest when you find a line with the key text.
Copy linkTweet thisAlerts:
@netbuddyOct 03.2006 — If you have access to a database, I would use that, Access type databased are fine for small number of users, some forum boards use them and can store a large number of items, off the top of my head I was quoted by some database guy that they are fine upto 10,000 enteries but generally get very slow when searching that many records which is why MySql wins hands down on Very Large Scale developments.

if you are using a flat sheet of 2000 enteries, this will be slow by comparison to using a database solution. Something you may want to attempt to set up as this will help you simplify your code and make it more portable when you need to change database types...
×

Success!

Help @Marlon_Br spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...