1848NC-CS,22113,80.00
2935NK3,30025,1.00
54P-CS,33050,10.00
63P-CS,11332,20.00
74P-CS,00000,000.00
86P-CS,22334,1.00
The above is my test file which is delimited by commas. I am matching on the first part of each record and then replacing the entire record when a macth is found with new data. The code I am using is:
$f1 = fopen("c:\Metrolabels\TextFile\UPC.txt", "r+t");
rewind($f1);
if ( $Action1A == 'Modify' ) {
$PartNbr = rtrim($PartNbr);
while (!feof($f1)) {
$holdf = fgets($f1);
$part1a = explode(',',$holdf);
$PartCmp = rtrim($part1a[0]);
$pointr = ftell($f1);
if ($PartCmp == $PartNbr) {
$lengthf = strlen($holdf);
$pointr = $pointr - $lengthf + 2;
fseek($f1,$pointr);
$holdrcd = rtrim($PartNbr) . ',' . rtrim($UPCode) . ',' . rtrim($Price);
$lengthr = strlen($holdrcd);
$holdf = $holdrcd;
fwrite($f1, $holdf, $lengthr);
break;
}
}
}
fclose($f1);
After executing the above code, here is what I see in notepad:
1848NC-CS,22113,80.00
2935NK3,30025,1.00
54P-CS,33050,10.00
63P-CS,11332,20.0074P-CS,10024,10.00.00
86P-CS,22334,1.00
As you can see, the 74P-CS is not on its own line. This is where the newline/carriage return should come in handy.