0

New to PHP and im reading a textfile into a string variable, removing double quotes and writing the contents back to a textfile. The problem is that the textfile, after saving, just consists of junk like this:

^@2^@0^@1^@0^@-^@1^@1^@-^@2^@0^@ ^@1^@3^@:^@5^@5^@,^@^@H^@K^@L^@M^@\^@S^@o^@f...

I am obviously doing something very wrong here.. Here is the code:

function replacequotes("myCommaSeparatedFile.csv")
{
    $rfile = fopen($filename, "r") or die("Unable to open file!");
    $outputfile = fopen("test2.csv", "w") or die("Unable to open file!");

    $readtext = fread($rfile, filesize($filename));
    $textoutput = str_replace('"', '', $readtext);

    echo $textoutput; // <- this shows ok on screen

    fwrite($outputfile, $textoutput);
    fclose($inpufile);
    fclose($outputfile);
}

Can someone help me out please, thanks.

2
  • Possible duplicate of PHP Create and Save a txt file to root directory Commented Aug 9, 2017 at 6:30
  • The "junk" you posted is not junk at all. It is text encoded as UTF-16BE. Open it in Notepad and it renders correctly. Commented Aug 9, 2017 at 7:54

1 Answer 1

1

Problem solved. dos2unix was a useful command. Thanks again Ali for the code cleaning!

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

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.