Forum Moderators: coopster

Message Too Old, No Replies

Creating paragraph breaks in form text area

         

neophyte

8:40 am on Aug 22, 2004 (gmt 0)

10+ Year Member



So, when you type into a text area in a form (like I'm doing now to create this post) if I hit return twice

to create a new paragraph

it works ... a new paragraph is created in which I can continue my latest question.

But...in my form it doesn't work. Got a form that's being inserted in a mySQL row, but when I view the form's contents (in html via php) all the paragraph breaks I've entered disappear and I get one long line of text.

of course, if I manually enter <br>'s into the text field where I want the breaks to occur, then the html formatting comes out correctly.

Do paragraph breaks disappear when a form's textarea is processed into a database field? Do I need to preform some additional processing to the database field before I bring it back into html?

God, please don't tell me I have to tell users to insert <br>'s into their text to create paragraphs!

When I previewed this post, all the paragraph breaks are shown where I double-returned.

What am I missing?

Neophyte

RonPK

8:47 am on Aug 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have a look at nl2br() [php.net]. It converts newlines to breaks.

dreamcatcher

8:49 am on Aug 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, the nl2br() function is what you are looking for. You can also use str_replace.

$message = str_replace("\r\n", "<br>", $message);

bedlam

10:47 pm on Aug 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Got a form that's being inserted in a mySQL row, but when I view the form's contents (in html via php) all the paragraph breaks I've entered disappear and I get one long line of text.

Heh ;)

And now that you're correcting it, feel free to consider yourself a superior programmer to whatever dolts did the feedback forms for the local telephone provider and a certain Mastercard provider (each company is one of the largest of its kind in Canada)...

I always wonder if their customer service reps think that ALL of the people who contact them via the internet are ignorant morons who don't know how to use the return key...

-B

jusdrum

4:25 pm on Aug 23, 2004 (gmt 0)

10+ Year Member



You can also use the following function to output valid XHTML, since the <br> tag is deprecated.

function prettyContent($Content)
{
// define ALLOWED_TAGS somewhere in your program
$ModifiedContent = strip_tags($Content,ALLOWED_TAGS);

// Consolidate line breaks
$ModifiedContent = preg_replace("/(\r\n¦\n¦\r)/", "\n\n", $ModifiedContent);
$ModifiedContent = preg_replace("/\n\n+/", "\n\n", $ModifiedContent);

// Wrap paragraphs with paragraph tags
$ModifiedContent = preg_replace('/\n?(.+?)(\n\n¦\z)/s', "<p>$1</p>\n", $ModifiedContent);

// Fix other tags that got wrapped in paragraph tags
$ModifiedContent = preg_replace('!<p>\s*(</?(?:code¦table¦tr¦td¦ul¦ol¦li¦pre¦select¦form¦blockquote¦dl¦dd¦dt¦p¦h[1-6])[^>]*>)!', "$1", $ModifiedContent);
$ModifiedContent = preg_replace('!(</?(?:code¦table¦tr¦td¦ul¦ol¦li¦pre¦select¦form¦blockquote¦dl¦dt¦dd¦p¦h[1-6])[^>]*>)\s*</p>!', "$1", $ModifiedContent);

// return the modified string
return $ModifiedContent;
}

bedlam

5:05 pm on Aug 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



...since the <br> tag is deprecated.

The <br> tag has not been deprecated in xhtml 1.0 transitional or strict. All that has changed is that it now must be closed:

This way:

<br> </br>
[which is not understood well by user agents]

Or this way:

<br/>
[which is also not well understood by user-agents]

Or this way:

<br />
[best for compatability]

If you're in doubt about the un-deprecated status of the <br /> tag, see the xhtml 1.0 strict annotated spec [w3.org], where you will find this:


<!ELEMENT br EMPTY> <!-- forced line break -->
<!ATTLIST br
%coreattrs;
>

-B

jusdrum

5:23 pm on Aug 23, 2004 (gmt 0)

10+ Year Member



I guess what I meant by deprecated is that it's not really a good way to structure a document. Wrapping paragraphs in paragraph tags is a much better way to go than putting <br /> tags in place of line breaks.

willybfriendly

5:52 pm on Aug 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try [webmasterworld.com...] where there is a pretty good discussion on this issue.

For properly formatted html, <p> tags ought to be used. This function comes out of the above thread:

function nl2p($html)
{
// Use \n for newline on all systems
$html = preg_replace("/(\r\n¦\n¦\r)/", "\n", $html);

// Only allow two newlines in a row.
$html = preg_replace("/\n\n+/", "\n\n", $html);

// Put <p>..</p> around paragraphs
$html = preg_replace('/\n?(.+?)(\n\n¦\z)/s', "<p>$1</p>", $html);

// convert newlines not preceded by </p> to a <br /> tag
$html = preg_replace('¦(?<!</p> )\s*\n¦', "<br />", $html);

return $html;
}

WBF

bedlam

6:04 pm on Aug 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I guess what I meant by deprecated is that it's not really a good way to structure a document.

Agreed - wholeheartedly agreed!

Wrapping paragraphs in paragraph tags is a much better way to go than putting <br /> tags in place of line breaks.

Can't agree with this though; not every linebreak signifies the beginning or end of a paragraph. Until the long awaited xhtml line element [w3.org] becomes available, I still think the best way of dealing with some markup problems is the much-maligned <br /> element:


<h1>One of Basho's Haiku<h1>
<h2>Translated by RH Blyth</h2>
<p>
Moonlight slants through<br />
The vast bamboo grove:<br />
A cuckoo cries
</p>

Arguably, this could be done by styling a particular class of paragraph's whitespace as preformatted, but I find this to be much less flexible.

-B

RonPK

6:17 pm on Aug 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Until the long awaited xhtml line element becomes available

I'd rather say "Until all your visitors use browsers that support the line element" - which may take a while ;)

willybfriendly

6:23 pm on Aug 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Long discussion about the merits of <br> here:

[webmasterworld.com...]

Poetry is one of those things that doesn't easily fit within the current document/tag specifications. A verse is not really a paragraph, so

<p>
Mary had a little lamb<br>
whose fleece was white as snow...
</P>

Isn't really accurate. Niether would be <li> or any other tag I am aware of. <pre> would appear to be the best way to go if one wants to be srtictly within current specs/recommendations.

WBF