By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,591 Members | 650 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,591 IT Pros & Developers. It's quick & easy.

how to remove multiple line breaks?

P: n/a
I'm am trying to remove multiple line breaks ("\n"). Here's my code:

$imageStr = trim($_POST['images']);
$imageStr = str_replace("\n\n", "\n", $imageStr);

----------

$_POST['images'] is a textarea field, and I am trying to remove extra
hard returns that a user types into the text area.

When I change the code to:
$imageStr = str_replace("\n", "%", $imageStr);
it works fine. So I know the function works and I have the right
character ("\n").

Anyone know what I'm doing wrong?
thanks much,
jon
jlarosa at alumni dot brown dot edu

Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

Jon LaRosa wrote:
I'm am trying to remove multiple line breaks ("\n"). Here's my code:

$imageStr = trim($_POST['images']);
$imageStr = str_replace("\n\n", "\n", $imageStr);

----------

$_POST['images'] is a textarea field, and I am trying to remove extra
hard returns that a user types into the text area.

When I change the code to:
$imageStr = str_replace("\n", "%", $imageStr);
it works fine. So I know the function works and I have the right
character ("\n").


How do you know it's NOT working??

What happens if you do $imageStr = str_replace("\n\n", "%",
$imageStr);?

Do two "\n"s get replaced by one "%"?

Ken

Jul 17 '05 #2

P: n/a
Jon LaRosa wrote:
I'm am trying to remove multiple line breaks ("\n"). Here's my code:

$imageStr = trim($_POST['images']);
/* while there are double line breaks in $imageStr */
while (strpos($imageStr, "\n\n") !== false) {

/* replace one such double line break by a single one */ $imageStr = str_replace("\n\n", "\n", $imageStr);


}

--
Mail to my "From:" address is readable by all at http://www.dodgeit.com/
== ** ## !! ------------------------------------------------ !! ## ** ==
TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
may bypass my spam filter. If it does, I may reply from another address!
Jul 17 '05 #3

P: n/a
Jon LaRosa wrote:
I'm am trying to remove multiple line breaks ("\n"). Here's my code:

$imageStr = trim($_POST['images']);
$imageStr = str_replace("\n\n", "\n", $imageStr);

----------

$_POST['images'] is a textarea field, and I am trying to remove extra
hard returns that a user types into the text area.

When I change the code to:
$imageStr = str_replace("\n", "%", $imageStr);
it works fine. So I know the function works and I have the right
character ("\n").

Anyone know what I'm doing wrong?
thanks much,
jon
jlarosa at alumni dot brown dot edu


$string=preg_replace('`[\r\n]+`',"\n",$string);

--
Justin Koivisto - sp**@koivi.com
http://www.koivi.com
Jul 17 '05 #4

P: n/a
.oO(Pedro Graca)
/* while there are double line breaks in $imageStr */
while (strpos($imageStr, "\n\n") !== false) {


IMHO it would be much easier to use a regular expression instead.

Micha
Jul 17 '05 #5

P: n/a
Pedro Graca wrote:
/* while there are double line breaks in $imageStr */
while (strpos($imageStr, "\n\n") !== false) {

/* replace one such double line break by a single one */


Oops: wrong comment. Should have been:

/* replace as many such double line breaks as possible
by a single one */
$imageStr = str_replace("\n\n", "\n", $imageStr);


}

--
Mail to my "From:" address is readable by all at http://www.dodgeit.com/
== ** ## !! ------------------------------------------------ !! ## ** ==
TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
may bypass my spam filter. If it does, I may reply from another address!
Jul 17 '05 #6

P: n/a
"Jon LaRosa" <jl*****@alumni.brown.edu> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I'm am trying to remove multiple line breaks ("\n"). Here's my code:

$imageStr = trim($_POST['images']);
$imageStr = str_replace("\n\n", "\n", $imageStr);

----------

$_POST['images'] is a textarea field, and I am trying to remove extra
hard returns that a user types into the text area.

When I change the code to:
$imageStr = str_replace("\n", "%", $imageStr);
it works fine. So I know the function works and I have the right
character ("\n").

Anyone know what I'm doing wrong?
thanks much,
jon
jlarosa at alumni dot brown dot edu


Linefeeds coming from the browser are "\r\n", which is why your replacement
code doesn't work. Use the snippet from Justin.
Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.