6

I have a PHP page which accepts input from user in a text area. Multiple strings are accepted as input from user & would contain '\n' and I am scanning it as:

$data = explode("\n", $_GET['TxtareaInput']);

Each string should be moved into the text file with new line character separation. This is the code I am using now and it separates each string with a '^M' character:

foreach($data as $value){
    fwrite($ourFileHandle, $value);
}

Is there anyway I can get each string followed by a carriage return?

5
  • ^M is a carriage return. Commented Aug 14, 2012 at 16:48
  • How do I scan for it using sed? Commented Aug 14, 2012 at 16:49
  • \r should be the proper escape sequence. Commented Aug 14, 2012 at 16:50
  • @nickb how do you get code style highlighting in the comments? Commented Aug 14, 2012 at 16:54
  • @ryanbwork wrap code in backquotes ` Commented Aug 14, 2012 at 16:57

3 Answers 3

63

You can simply write it back using implode:

file_put_contents('file.csv', implode(PHP_EOL, $data));
Sign up to request clarification or add additional context in comments.

1 Comment

This is the way
2

Try this:

$data = explode("\n", $_GET['TxtareaInput']);
foreach($data as $value){
    fwrite($ourFileHandle, $value.PHP_EOL);
}

10 Comments

No you will not see ^M in the file. Do you need to see it?
^M still appears in the file. Any ideas why it can be seen?
Can you give a bit of info about the env/ide involved? what OS are you running this/generating the file in? and what editor are you using to view the file which shows up ^M?
I am using red hat linux and vim editor. I am not using any IDE for PHP and HTML.
vi editor shows special character such as carriage returns(^M) and link breaks($) in the editor. In order to hide the editor showing this information, type in the following vim command in the editor and press enter :set nolist. This will suppress showing those special characters. To see them again just do a :set list This can be seen in vim because, being the powerful editor that it is, it provides such functionalities. This will not affect the content in your file in anyway..
|
1

If you want to add new lines, then why are you first removing them?

$data = explode("\n", $_GET['TxtareaInput']);

Keep only this line:

fwrite($ourFileHandle, $data);

It will write your data to the file as it was received.

If you want to replace all new lines by carriage returns before writing to file, use this code:

    fwrite($ourFileHandle, str_replace("\n", "\r", $data));

3 Comments

This will not port between windows (\r\n) and *nix (\n)
@Raidenace: The OP asked "Is there anyway I can get each string followed by a carriage return?". My code does exactly that.
Does this work ? fwrite($ourFileHandle, $data); Does fwrite's second argument can be array? I think it must be a string and explode gives an array.

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.