I am appending something to each line in a file and then writing that to a new file.
But when I looked at the new file what it is doing is writing the line from the old file then writing what it should be appending to that line on a new line.
So if the first line in the old file is
A
The first and second line in the new file is
A
/appendage
What it should be is one line:
A /appendage
How can I fix my code so that the appendages are on the same line as what they are being appended to?
function writeTag($tags, $line, $file){
foreach($tags as $t){
$line .= " /".$t['tag']." ";
fwrite($file, $line);
}
}
$tagger = new PosTagger('lexicon.txt');
$lines = file('dictionary.txt');
$out = fopen("tagged.txt", "w");
foreach($lines as $line){
$tags = $tagger->tag($line);
writeTag($tags, $line, $out);
}
Update:
With bwoebi's updated answer adding IGNORE_NEW_LINES, the output file I am getting looks like:
A /DT A$ /DT A&E /DT A&E /DT /NN A&M /DT A&M /DT /NN
A&P /DT A&P /DT /NN
What I am looking for is:
A /DT
A$ /DT
A&E /DT
A&E /DT /NN
A&M /DT
A&M /DT /NN
A&P /DT
A&P /DT /NN