0

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!

2 Answers 2

1

Example code is pretty unfinished, so I will make suggestions about your main problem only - why there are 2 strings.

That is probably because you have such string in the input: xxx-110K Run \nblk-Sm;XXXXXXXBlack;black;; ;mens;;... But your text editor don't show this as separate strings. This happens with different combiantions of \n\r or \r\n or \n

You can delete all "\r" in first, to check if it is a case.

Trim all values in $rows also can help sometimes.

Sign up to request clarification or add additional context in comments.

2 Comments

Actually added \r and it worked. Didn't think about trying it until you mentioned it. Thanks!
Answer isn't necessarily correct. Comment said to delete all "\r" when in fact, the solution was to add "\r". I gave credit as correct answer because A. Denis made me think of it. I actually showed the correct answer in my post below.
0

Solved.

Changed

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

To

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

Thanks for the help!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.