473,574 Members | 2,269 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 13242

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($imageS tr, "\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_re place('`[\r\n]+`',"\n",$strin g);

--
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($imageS tr, "\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($imageS tr, "\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.goo glegroups.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
3217
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
5092
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 doesn't work. QUESTION: How can I prevent the line wrap/break in the html source text? The only thing I've seen is <nobr> but that doesn't...
4
22743
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 to export the report? I tried exporting a report by using the .rtf rich-text format (the plain-text format was the only other word-processing...
4
1741
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 website had just gotten a revision, but the site was an utter mess, so I've been trying to fix it up. As I've gone along, I've learned some aspects...
5
16831
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 line breaks with <br /> tags ( using the php function nl2br() ), and <br /> tags showed up in the textarea.
6
2467
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 collection". I suspect it has something to do with parameters in the query I'm calling. I need to pass an object on the form to a parameter in the...
2
16859
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? No matter the width of the template column width, they alyways render in seperate lines which alters the row height. thank you in advance ...
1
2504
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 machines can then pick up and process. Now the guy who wrote one of the reports is obviously a bit clueless and the certain lines don't match up so for...
5
5812
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 info is currently stored in a single cell using line breaks and I want to seperate the info in each line break into specific cells. Example: red
0
7826
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7741
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8085
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8259
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7838
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8126
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5328
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2257
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1090
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.