Page 1 of 1

damn darn carriage return

Posted: Mon Jun 09, 2003 9:05 am
by Deddog
Here's the scenario. I need to strip out any carriage returns before a field is submitted into my database. The field in question is used to populate “OverLib”(JS app used to generate pop over windows)

If there are any Carriage returns within the string given to “OverLib” the JS has a wobbly. Plus the carriage returns are untidy.

Here are my unsuccessful efforts to strip out the offending carriage returns.

Does anyone have the answer?

1. $New = str_replace('\r', '', $New);
2. $New = str_replace('\n', '', $New);
3. $New = str_replace('^M', '', $New);
4. $New = str_replace('
’, '', $New);

using php

Posted: Mon Jun 09, 2003 9:06 am
by Deddog
sorry - using PHP

Posted: Mon Jun 09, 2003 9:13 am
by sleepingdanny
Well, you can trim \n, \r with the trim() function...

str_replace("change","to this",$in_this)

Posted: Mon Jun 09, 2003 9:42 am
by patrikG
Might be worth pulling out the old nl2br - I am not sure how clever the function is though. If it really replaces everything with "<br />" then use that as a placeholder which you replace.

What is quite interesting are the usernotes to nl2br in the manual:
Macintosh: \r
Unix : \n
Windows : \r\n
The code below allows the line endings to be easily replaced with a br tag on any of the three major OSs.
-zak@php.net]
Well, there's some major issues with NewLine codes vs OS (win, linux, mac). Here is what I use : $s_data = ereg_replace("(\r\n|\n|\r)", "<br />", $s_data);
So far it worked great for me.
If all else fails, try the ereg_replace above (slighlty modified I suppose). :)

You beatuie

Posted: Mon Jun 09, 2003 10:09 am
by Deddog
Nice one patrikG!

$New = ereg_replace("(\r\n|\n|\r|^M)", "", $New);

did the job.

Danny,
thanks for the help. As far as i am aware "TRIM" only strips from the end and the beginig of a string.

Thanks again,

Deddog.