Welcome to WebmasterWorld Guest from 54.146.49.147

Forum Moderators: incrediBILL

Message Too Old, No Replies

How to view carriage return code

\r, \n, \r\n, etc

     
1:37 pm on Aug 26, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:May 6, 2008
posts:2011
votes: 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)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts:3123
votes: 0


I've replaced \n, \r, \r\n ...


These are the character escapes, representing character codes 10 (LF) and 13 (CR) in decimal. There are no others. How are you replacing these in the submitted string? JavaScript, PHP? Presumably this is in a textarea control?
6:44 pm on Aug 26, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:May 6, 2008
posts:2011
votes: 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)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 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
2,Joe,"Some text",456
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)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13210
votes: 347


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)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 0


Isn't \r\n redundant?


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)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13210
votes: 347


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)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 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)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:May 6, 2008
posts:2011
votes: 0


Found the problem. Was using ' instead of " in my str_replace function. How stupid of me. Thanks to everyone for the help.