By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,729 Members | 1,386 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,729 IT Pros & Developers. It's quick & easy.

reading/writing a file?

P: n/a
I've got this text file full of lines like:

A*1King Richard Dale*3Welton Orchard Rd*4Petersburg*5257-1234

the columns are defined by the *#, so that line would break apart
like:
A*1King Richard Dale *3Welton Orchard Rd *4Petersburg *5257-1234
Basically, what I need to do is read this file and I need to write
field 1, 4 and 5 which need to go into a text file in tab delimited
format like:

King Richard Dale Petersburg 257-1234

What can I do to get this task accomplished? I'm familiar with
requesting and manipulating data from mysql but this reading/writing
to a txt file is completely new to me. I have about 1 years worth of
off and on PHP experience. Any help is greatly appreciated. Thank you

Jason
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Dot Kom" <jr*****@hardynet.com> wrote in message
news:5c**************************@posting.google.c om...
I've got this text file full of lines like:

A*1King Richard Dale*3Welton Orchard Rd*4Petersburg*5257-1234

the columns are defined by the *#, so that line would break apart
like:
A*1King Richard Dale *3Welton Orchard Rd *4Petersburg *5257-1234
Basically, what I need to do is read this file and I need to write
field 1, 4 and 5 which need to go into a text file in tab delimited
format like:

King Richard Dale Petersburg 257-1234

What can I do to get this task accomplished? I'm familiar with
requesting and manipulating data from mysql but this reading/writing
to a txt file is completely new to me. I have about 1 years worth of
off and on PHP experience. Any help is greatly appreciated. Thank you

Jason


This can be solved in a few steps, but you'll need to read up on the
associated functions yourself.

Step 1: If the file is not too large, you can load it into an array variable
with the "file" function:

$array_data = file("/www/htdocs/data/thefile.dat");

Step 2: You can slice up each line of the file at the delimeters with the
"explode" function:

foreach ($array_data as $array_line) {
$array_elements = explode("*",$array_line);
// $array_elements[0] would have "A"
// $array_elements[1] would have "1King Richard Dale", etc.
// Process items here; remember to extract the leading number character(s)
}
Jul 17 '05 #2

P: n/a
jr*****@hardynet.com (Dot Kom) wrote in message
news:<5c**************************@posting.google. com>...

I've got this text file full of lines like:

A*1King Richard Dale*3Welton Orchard Rd*4Petersburg*5257-1234
Wow... A weird setup, isn't it?
Basically, what I need to do is read this file and I need to write
field 1, 4 and 5 which need to go into a text file in tab delimited
format like:

King Richard Dale Petersburg 257-1234

What can I do to get this task accomplished?


Something like this:

$in = fopen ('your_old_file.txt', 'r');
$out = fopen ('your_new_file.txt', 'w');
if (!$out) {
die ('Cannot open the output file for writing. ');
}
if (!$in){
die ('Cannot open the input file for reading. ');
}
while (! feof ($in)) {
$items = explode ('*', fgets ($in, 4096));
$line = '';
foreach ($items as $item) {
$num = $items{0};
if (($num == 1) or ($num == 4)) {
$line .= substr ($item, 1) . "\t";
}
if ($num == 5) {
$line .= substr ($item, 1) . "\r\n";
}
}
if ($line <> '') {
fputs ($out, $line);
}
}
fclose ($in);
fclose ($out);

Cheers,
NC
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.