Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to remove "carriage return" entries in a string 1

Status
Not open for further replies.

ChrisMacPherson

Programmer
Jul 3, 2000
157
GB
Hi All,

For my next problem,

I need to strip all carriage return occurences from a string. I cant seem to find a specific method to do this, and I dont know what characters to strip if I'm just going to remove the characters using a normal string method.

Any ideas?

Thanks for any help. Chris MacPherson
macpweb@hotmail.com
Bring on the new Browza's!!
 
Well, what type of returns are they? \n or \r?

You could use several functions.

Since you are programming in PHP, and will most likely output this to a browser, you can use the nl2br() function to convert all new lines to a [tt]<br>[/tt] tag.

Or you could do a simple regular expression:

[tt]
$string = eregi_replace(&quot;\r&quot;,&quot;&quot;,$string);
$string = eregi_replace(&quot;\n&quot;,&quot;&quot;,$string);
[/tt]

Hope this helps.

-Vic

vic cherubini
krs-one@cnunited.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash
====
 
First let me apologize to vcherubini fpr this cotradiction.

'you can use the nl2br() function to convert all new lines to a <br> tag.'

nl2br() does not convert the newline characters, it merely places a <BR> before each newline character in the string. The newline characters remain in the string.


He has shown you how to replace them with eregi_replace. Another option you may consider is addcslashes().

It won't remove them, but it will delimit the characters so that it is safe to write to a flat file or what have you.

addcslashes ($not_escaped, &quot;\0..\37&quot;)

I believe that is the correct sytax. This will delimit any characters with an ASCI value in the range 0 to 37. That should catch newline and carriage returns. :cool:
 
Thanks for the help,

In response to Vic, I am not sure which it is:

>> &quot;Well, what type of returns are they? \n or \r?&quot;

Are they for windows and Unix, or are they both different types of Windows/Dos carriage returns?

I dont use Unix at all, so I have no idea about that side of it at all. It would be interesting to know though as I keep reading about being carefull about the different characters used by the two systems.

I'll try the code and tell you how it went

Thanks again Chris MacPherson
macpweb@hotmail.com
Bring on the new Browza's!!
 
Hi there

I've tried both the regular expression and addcslashes methods shown above to get rid of a carriage return and a linefeed in a string.

However when I do the following in a table:

echo &quot;<tr><td><textarea name=\&quot;moddloadtop\&quot; cols=40 rows=4>&quot;.$modtop.&quot;</textarea><br><br></td></tr>&quot;;

I find that instead of getting just the text output by $modtop I also get the carriage return and linefeed.

It's not the problem of the <textarea> tag as <textarea>sometext</textarea> shows no CR or LF.

I've tried:

$modtop = eregi_replace(&quot;\r&quot;,&quot;&quot;,$modtop);
$modtop = eregi_replace(&quot;\n&quot;,&quot;&quot;,$modtop);

and

addcslashes ($modtop, &quot;\0..\37&quot;)

and

$modtop = eregi_replace(chr(10),&quot;&quot;,$modtop);
$modtop = eregi_replace(chr(13),&quot;&quot;,$modtop);

but none get rid of the CR and LF in the textarea.

Any thoughts would be great!
 
You could try this, though I don't think it would make a difference:
Code:
$modtop = str_replace(&quot;\n&quot;, &quot;&quot;, str_replace(&quot;\r&quot;, &quot;&quot;, $modtop));
My guess is that there is something else that has the CR LF, maybe the browser automatically appends it for some reason.
Also, what does $modtop contain? //Daniel
 
I've checked - the browser doesn't append it as

<textarea>something</textarea>

doesn't have the CF + LF but

echo &quot;<textarea>&quot;.$modtop.&quot;</textarea>&quot;;

does.

The weird thing is that if I do:

$modtop = eregi_replace(&quot;\r&quot;,&quot;R&quot;,$modtop);
$modtop = eregi_replace(&quot;\n&quot;,&quot;N&quot;,$modtop);

I get the text string in $modtop then a CR then &quot;N&quot;:

sometext
N

Clearly the \n is being replaced ok but the \r isn't.

Please keep the thoughts coming on this one :)

Thanks muchly!
 
How about:

[tt]
Code:
$foo = preg_replace ('/\r|\n/', '', $bar);
[/tt]

or perhaps a little more correctly would be to replace any of &quot;\r\n&quot;, &quot;\r&quot;, or &quot;\n&quot; with a single space:

[tt]
Code:
$foo = preg_replace ('/\r\n|\r|\n/', ' ', $bar);
[/tt]
______________________________________________________________________
TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top