I am trying to remove the escape sequences present in between my output. I am coding using php and I have output from the function and it contains escape sequences like \r
and \n
. How do I escape these stuff from output so that I get a proper output. The issue is that I am trying to add this output to a csv file, so it will take \n
as next line.
Asked
Active
Viewed 1.6k times
1

gherkins
- 14,603
- 6
- 44
- 70

user1371896
- 2,192
- 7
- 23
- 32
-
It has nothing to do with the way I process the data... Im just taking that from the database. I get that in the form "Randall \r\nStephenson" – user1371896 Jul 25 '12 at 05:01
4 Answers
2
Single quotes inhibit escape sequences. '\r'
is two characters, \
and r
; "\r"
is CR.

Ignacio Vazquez-Abrams
- 776,304
- 153
- 1,341
- 1,358
1
Use str_replace function:
$your_string = "here is your string"
$your_string = str_replace("\n", "", $your_string);
$your_string = str_replace("\r", "", $your_string);

levi
- 22,001
- 7
- 73
- 74
0
This post indicates that keeping the newline in the field should work, so long as:
- newlines inside cells are represented as \n (
str_replace("\r\n", "\n", $cell)
) - cells are quoted
- rows are terminated with \r\n
If you're not already I'd recommend using fputcsv(), as it will take care of the quoting for you.