473,786 Members | 2,380 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 13249

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
3231
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
5108
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 affect the source, just the output...is there ANY way?
4
22771
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 option listed when exporting). I then opened the .rtf file in Word. However, it looks like some...
4
1759
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 of PHP, but my knowledge is still very limited. Here is the problem I'm trying to fix. A backend...
5
16852
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
2482
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 query, and the query is quite complex as well. Here is the query:
2
16865
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 <EditItemTemplate>
1
2521
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 example our % line is a couple of spaces too far across. A09 62371 __7.20%
5
5824
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
10360
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10163
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10108
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9960
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.