I have a text file that has a name type of data on every line then 2 blank lines and then another couple lines of data (not the same number)
so it looks like this:
Name
Street
Address
Phone Fax
Website
Name2
Street2
Address2
Phone Fax2
Website2
comments2
Name3
Street3
Address3
Phone Fax3
Website3
And i want to take that data and make it into a txt file line seperated by a | (not a comma since commas are in the adress fields)
so it would read
Name|Street|Address|Phone Fax|Website
Name2|Street2|Address2|Phone Fax2|Website2|comments2
Name3|Street3|Address3|Phone Fax3|Website3
Any help would be appreciated
Need help sorting a txt files contents
Moderator: General Moderators
Maybe something along the lines of:
Code: Select all
<?php
define('INFILE', 'test.txt');
define('OUTFILE', 'test2.txt');
$count = 0;
foreach(file(INFILE) as $line) {
$line = trim($line);
if(!empty($line)) {
$outputї$count]ї] = $line;
} else {
$count++;
}
}
if(!empty($output)) {
foreach($output as $line) {
$newfileї] = join('|', $line);
}
}
file_put_contents(OUTFILE, join("\n", $newfile));
?>- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Awesome, Thank you!
Awesome, worked right out the box