damn darn carriage return

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Deddog
Forum Commoner
Posts: 55
Joined: Thu Sep 26, 2002 6:05 am
Location: Brighton

damn darn carriage return

Post 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);
Deddog
Forum Commoner
Posts: 55
Joined: Thu Sep 26, 2002 6:05 am
Location: Brighton

using php

Post by Deddog »

sorry - using PHP
sleepingdanny
Forum Newbie
Posts: 12
Joined: Thu Mar 20, 2003 4:27 am
Location: Israel

Post by sleepingdanny »

Well, you can trim \n, \r with the trim() function...

str_replace("change","to this",$in_this)
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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). :)
Deddog
Forum Commoner
Posts: 55
Joined: Thu Sep 26, 2002 6:05 am
Location: Brighton

You beatuie

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