Search code examples
phpnewlinefwritefgets

php fgets, fwrite and newline


Using php fgets function , I am reading from one file, file "A" which is in a particular format and I am writing to another file, file "B" in a particular format using fwrite. However, no matter what new lines or line breakers I put, some characters keep being misplaced. Below is the format I wish to see in the new file, file B:

ICCID,IMSI,KI,ESN,PIN1,PUK1,PIN2,PUK2,IMSI2,KI2,ACC_NBR,NAI_USERNAME,NAI_PASS
8926003010422000616F,,,645030142200061,2064,16002217,4029,34594354,,,,,
8926003010422000624F,,,645030142200062,8678,91445678,5351,06417774,,,,,
8926003010422000632F,,,645030142200063,0356,51052167,6976,27210792,,,,,
8926003010422000640F,,,645030142200064,5504,38104570,4917,61385706,,,,,
8926003010422000657F,,,645030142200065,7158,23625228,2726,13487033,,,,,
8926003010422000665F,,,645030142200066,3922,52488665,6014,33705660,,,,,

BUT below is exactly what I am getting:

ICCID,IMSI,KI,ESN,PIN1,PUK1,PIN2,PUK2,IMSI2,KI2,ACC_NBR,NAI_USERNAME,NAI_PASS
8926003010422000608F,,,645030142200060,3741,26564507,7283,13507659
,,,,,8926003010422000616F,,,645030142200061,2064,16002217,4029,34594354
,,,,,8926003010422000624F,,,645030142200062,8678,91445678,5351,06417774
,,,,,8926003010422000632F,,,645030142200063,0356,51052167,6976,27210792
,,,,,8926003010422000640F,,,645030142200064,5504,38104570,4917,61385706
,,,,,8926003010422000657F,,,645030142200065,7158,23625228,2726,13487033
,,,,,,,,,,,,,,,,,

you will notice the characters ,,,,, get put at the start of a line rather than the end where I want them to be. Notice also the last line gets to be ,,,,,,,,,,,,,,,,,. Not needed at all.

Below is the code that reads and writes. Needing someone to help me sort out this.

    $headStockin= "ICCID,IMSI,KI,ESN,PIN1,PUK1,PIN2,PUK2,IMSI2,KI2,ACC_NBR,NAI_USERNAME,NAI_PASSWORD"."\r\n";
    $file = fopen($inputFile, "r");  // reading input file
    $myStockinfile = fopen($inputFile."_StockIn.txt", "wb") or die("Unable to create/open a file!"); 
    fwrite($myStockinfile, $headStockin);
    $thr=",,,";
    $one=",";
    $fiv=",,,,,";

    $lineNo = 0;
    $startLine = 21;

    while(!feof($file)){
        $lineNo++;
        $line = fgets($file);

        if ($lineNo >= $startLine) {
            $result = explode(" ", $line);
            $in= $result[2].$thr.$result[1].$one.$result[3].$one.$result[5].$one.$result[4].$one.$result[6].$fiv;
            echo $in;   //to see output on html
            fwrite($myStockinfile,$in);
        }

    }
    fclose($file);
    fclose($myStockinfile);

}



INPUT FILE BELOW

    *
********************************************************************************
* HEADER DESCRIPTION
********************************************************************************
*
Quantity:   5000
*
********************************************************************************
* INPUT VARIABLES
********************************************************************************
*
var_in_list:
IMSI:   645030142200060
Ser_Nb: 8926003010422000608F
*
********************************************************************************
* OUTPUT VARIABLES
********************************************************************************
*
var_out:MSISDN/IMSI/ICCID/PIN1/PIN2/PUK1/PUK2
+260950605404 645030142200060 8926003010422000608F 3741 7283 26564507 13507659
+260950605411 645030142200061 8926003010422000616F 2064 4029 16002217 34594354
+260950605412 645030142200062 8926003010422000624F 8678 5351 91445678 06417774
+260950605416 645030142200063 8926003010422000632F 0356 6976 51052167 27210792
+260950605418 645030142200064 8926003010422000640F 5504 4917 38104570 61385706
+260950605421 645030142200065 8926003010422000657F 7158 2726 23625228 13487033

Solution

  • The misplaced commas are because fgets() returns a string that includes the newline at the end. So $fesult[6] has a newline at the end, so $fiv gets put onto the next line. The solution is to trim the input before exploding, and then write a newline in your output.

    The extra line is because you're testing for EOF before reading the line. feof() isn't detected until after you read the end of the file.

    So change your loop to:

    while ($line = fgets($file)) {
        $lineNo++;
        if ($lineNo < 21) { // Skip first 20 lines
            continue;
        }
        $line = rtrim($line);
        $result = explode(" ", $line);
        $in= $result[2].$thr.$result[1].$one.$result[3].$one.$result[5].$one.$result[4].$one.$result[6].$fiv.PHP_EOL;
        echo $in;
        fwrite($myStockinfile, $in);
    }