Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Line Breaks

Status
Not open for further replies.

jollydonkey

Technical User
Sep 5, 2008
23
CA
Hello,

I'm having some issues with properly formatting text extracted from an online form using a textarea box. Output will be a plain text email.

Here's what I need:
-- text should wrap at 72 characters
-- for lengthy lines, breaks (\n) should only occur after 72 characters
-- if a user creates a hard return or inserts a blank line in the textarea box, breaks should occur at that point.

For example, if the input is:
Code:
The lazy fox jumped over the cow. The lazy
fox jumped over the cow.[hard return]
[hard return]
Baa Baa[hard return]
Black Sheep.

The output should be:
Code:
The lazy fox jumped over the cow. The lazy fox jumper over the cow...72characters\n\n

Baa Baa\n
Black Sheep.

Is there a simple solution here? I've tried using wordwrap and nl2br as follows:
Code:
[red]
$string = wordwrap($string, 72, "\n");
$string = nl2br($string);
[/red]
Any help would be greatly appreciated. Be gentle - I'm a newbie :)
 
wordwrap() sounds right.
the nl2br is only useful for html. if you're using plain text then using this function will undo the goodness in the first function.

you're going to run into problems with different browser implementations of textarea boxes. nothing much you can do there other than try to normalise with a flash or js based equivalen.

 
What is the output you're getting? What does the input look like before you start manipulating it? It seems to work for me, but I'm not using a browser.

Code:
[axtell@Axtell4 ~]$ cat test.php
<?php
$input ="The lazy fox jumped over the cow. The lazy fox jumped over the cow.The lazy fox jumped over the cow.\n\n\n Baa Baah \n Black Sheep";
$string = wordwrap($input,72);
$string = nl2br($string);
echo $string;
?>
[axtell@Axtell4 ~]$ php test.php
The lazy fox jumped over the cow. The lazy fox jumped over the cow.The<br />
lazy fox jumped over the cow.<br />
<br />
<br />
 Baa Baah <br />
 Black Sheep[axtell@Axtell4 ~]$

-----------------------------------------
I cannot be bought. Find leasing information at
 
The issue is that the width of the textarea box is restrictive. So if the user inputs text, (\n) newline codes are sent to php at the point where the text wraps. The resulting output is fragmented.

Perhaps my strategy should be to remove newlines, add newlines at character position 72, however, how do I solve for blank lines or hard carriage returns in the textarea box so the output is legible?

Given that nl2br is strictly, HTML, I think I shouldn't be using it (as I only need plain text output).

Thanks for the help!

Cheers,
jd.
 
The issue is that the width of the textarea box is restrictive. So if the user inputs text, (\n) newline codes are sent to php at the point where the text wraps. The resulting output is fragmented.

only with some browsers. hence my comment. you need to test across all your target browsers to see which way each behave. or normalise using a js/flash alternative.
 
jollydonkey,

This is one of several posts where you find yourself in a jam with having to display content with a fixed width of 72 characters.

Perhaps, if you were to post:
1) Code
2) Snapshot of expected output

one can give you better advise as one may better understand your objective visually.

Further more, even if you pad your string to be 72 characters in length, the font type will determine column alignment.

If you were to display the content off a <textarea> field within a container with a fixed width, the content should break/wrap around that width. Knowing this, why work so hard on stripping the field into multiple lines of exactly 72 characters?

I really believe that more and better detailed information is needed to really give you a true and clean solution.



--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Hi,

I think I figured it out. Here was the initial issue:

I'm having some issues with properly formatting text extracted from an online form using a textarea box. Output will be a *** plain text email***.

-- text should wrap at 72 characters
-- for lengthy lines, breaks (\n) should only occur after 72 characters
-- if a user creates a hard return or inserts a blank line in the textarea box, breaks should occur at that point.


My solution is:
-- using html, the textarea box should have the attribute wrap="virtual"
-- the resulting php code should be:
Code:
$string = $_POST['string' ];
$string = wordwrap($string, 72, "\n");

Thank you everybody for helping think this one through and steer me in the right direction!
 
i hope that works in all browsers! i have seen that fix for safari in the past. have you tested the others?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top