472,143 Members | 1,586 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,143 software developers and data experts.

how to remove multiple line breaks?

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
6 13094

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

Similar topics

2 posts views Thread by revjjjames | last post: by
4 posts views Thread by m.shidoshi | last post: by
5 posts views Thread by joelbyrd | last post: by
2 posts views Thread by Jerod Hatley | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.