473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 13214

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: revjjjames | last post by:
Hello - What is the correct expression to remove any extra line breaks beyond two in a string? Sincerely, Jim
1
by: KathyB | last post by:
Hi, I'm using xml transformed to html via xsl. The problem is my HTML source text auto wraps/breaks lines. This only matters when the line involves a parameter I'm passing to a script...which then...
4
by: intl04 | last post by:
I have a memo field that is included in some Access reports I created. Is there some way for the memo field to display nicely formatted text, with line breaks between paragraphs? Or is it necessary...
4
by: m.shidoshi | last post by:
I was recently put in charge of heading up my company's website, and while I have a lot of experience on the design side of things, I'm still very new to the programming side. When I started, the...
5
by: joelbyrd | last post by:
Didn't know exactly where to post this, but: How do I get line breaks in a textarea? I'm pulling text from a database, and this text definately has line breaks in it, because I replaced all the...
6
by: kenshiro | last post by:
Hi All, I'm having a problem with some VBA code in one of my Access 2003 databases. I'm getting the following error when running code behind a command button on a form: "Item not found in this...
2
by: Jerod Hatley | last post by:
when I try to add two controls to an item template, I cannot figure out how to keep them on a single line. the functionality is just fine but how do I get the controls to render on a single line?...
1
by: buggtb | last post by:
Hi Guys, I've been given the joyous task of updating some very old scripts at work and I could do with a little help. Our Unix system dumps text files to pseudo spoolers that our windows...
5
by: jbedwe02 | last post by:
Hi all, I have no clue how to slove this problem & am desperate for an answer: I'm creating a spreadsheet and need to now how to seperate data within a single cell into multiple cells. The...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.