homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / HTML
Forum Library, Charter, Moderators: incrediBILL

HTML Forum

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 ...

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)

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

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)

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)

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)

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.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / HTML
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved