|How to view carriage return code|
\r, \n, \r\n, etc
| 1:37 pm on Aug 26, 2011 (gmt 0)|
I have a text field where sometimes the user hits enters in their sentences. I want that carriage return removed. The problem is, I can't! I've replaced \n, \r, \r\n, and some others, but nothing is working!
If I could SEE what the carriage return code was, I could remove it, but it's stored in the database undetected and outputted undetected. Has anyone had this problem and can help?
| 2:47 pm on Aug 26, 2011 (gmt 0)|
|I've replaced \n, \r, \r\n ... |
| 6:44 pm on Aug 26, 2011 (gmt 0)|
I'm putting the input of a textarea into a database. When taking those results out of the database and moving them to a .csv file, the carriage returns are there as they cause the output to go to the next excel row, ruining my formatting.
| 8:22 pm on Aug 26, 2011 (gmt 0)|
|...moving them to a .csv file, the carriage returns are there as they cause the output to go to the next excel row |
Excel should be able to handle this OK, providing the field that contains newlines is surrounded by double quotes AND there is no space surrounding the comma.
Excel should see the following as just 3 rows (file called sample.csv).
1,Bob,"This is a long bit of text
with some newlines",123
3,Fred,"Nice long bit of text
again with 2 newlines
in the middle",789
Or would you still prefer to get rid of the newlines in the text fields? In which case, the following should replace all newlines (whatever they might be) with spaces using PHP...
$textfield = str_replace(array("\r\n", "\n", "\r"), ' ', $textfield);
| 9:03 pm on Aug 26, 2011 (gmt 0)|
Isn't \r\n redundant? Or does it save time and resources to grab them together when they occur together?
Technically there are characters other than CR and LF that can express a physical linebreak. (For some reason that now escapes me, I was reading just yesterday about white space in HTML.) But unless your users have weird computers, you may never see them.
| 10:31 pm on Aug 26, 2011 (gmt 0)|
If you only check/replace the individual characters (\n and \r) then you'd end up with 2 spaces (in this example) for each occurrence of \r\n. If you were replacing newlines with '<br>' and then it would be more obvious.
| 1:00 am on Aug 27, 2011 (gmt 0)|
D'oh! I missed the ' ' part, even though I do the identical thing myself in a different context. Can you do RegExes? You'd really want \s+ to allow for literal spaces before line breaks.
| 9:09 am on Aug 27, 2011 (gmt 0)|
Replacing with spaces was just my assumption. StoutFiles only talks of 'removing' the newlines. However, it seems the real problem is that the newlines are breaking a CSV file when imported into Excel. But (as in my post above) if the CSV file is formatted correctly then Excel should be able to import it OK, newlines and all. (Unless, the newlines are completely superfluous and the user has entered them in error and they should be removed?)
If you just want to replace newlines en masse (and no other trickery) then I think str_replace() would be preferable - it will be more efficient than a regex. \s only matches \n and \r in terms of newline characters.
| 12:36 pm on Aug 29, 2011 (gmt 0)|
Found the problem. Was using ' instead of " in my str_replace function. How stupid of me. Thanks to everyone for the help.