Skip to content
Advertisement

php array inserting line break when writing to text file

When parsing a text file full of variables (one on each line, no whitespace after), after processing, it’s being written to a $myfile with an additional line after.

Please checkout my code:

$txt_file    = file_get_contents('spins/' . $skustart . '/' . $skustart . '.txt');
$rows        = explode("n", $txt_file);
array_shift($rows);
$row_data = $data;
foreach($rows as $row => $data)
{
    //parent

    $txt = $skustart. "-" . $a.$skip1.$slogan . " " . $data . $sloganb . ' Tee Shirt, Men's ' . $skip3 . $brand . $desc . $value . $tee .  $item_type . $skip20 . $skip9 .  $bullet_point1 . $bullet_point2 . $value . $bullet_point212 . $bullet_point3 . $bullet_point4 . $bullet_point5 . $value  . $gen_k1 . $value  . $gen_k2 .  $value  . $gen_k3 .  $value  . $gen_k4 .  $value  . $gen_k5 . $main_image_url1 . $img_folder_name . "/" . $skustart. "-" . $a . '-black' . $main_image_url1_format . $other_image_url1 . $skip10 . $parent . " ; ;" . $variation_theme . $skip18 .  $department_name .  " ; ;" . $skip2 .$skip2 .  $skip18 . "n"; //this is the parent
    fwrite($myfile, $txt);


    //end parent

Here is what is spit out:

Note: 10K Run is the variable in question ($data)

xxx-110K Run
blk-Sm;XXXXXXXBlack;black;; ;mens;; ;; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;Small;Small;

Where it should be:

xxx-110K Run blk-Sm;XXXXXXXBlack;black;; ;mens;; ;; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;Small;Small;

I’m not sure what is causing this, the code seems to work fine until I implement it into this generator.

Please advise or provide any suggestions, thank you very much!

Advertisement

Answer

Solved.

Changed

$rows        = explode("n", $txt_file);

To

$rows        = explode("rn", $txt_file);

Thanks for the help!

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement