Forum Moderators: coopster

Message Too Old, No Replies

Carriage returns in text areas

How to recognize carriage returns received from a text area

         

Solta

3:40 am on May 10, 2003 (gmt 0)

10+ Year Member



Hi, I'm new to PHP and have a quick question

I have a form in an html document where users submit short articles in a text area to editors of a news-based site. I know how to set up the form to create an html/php document with the information submitted, but how do I get the carriage returns in the text area to transfer to <p></p> or <br> tags in html. For example, when submitting these posts here, the carriage returns are recognized.

Another question. Do I have to do anything special to the text area which would allow users to include html tags for formatting?

thanks,
-Solta

daisho

4:09 am on May 10, 2003 (gmt 0)

10+ Year Member



but how do I get the carriage returns in the text area to transfer to <p></p> or <br>

$html_str=str_replace("\n","<br>",$_REQUEST['textarea_field']);

Do I have to do anything special to the text area which would allow users to include html tags for formatting?

No. You have to do something special to make sure they can't :)

daisho.

grahamstewart

9:55 am on May 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this little lot...


// 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);

This will surround blocks of text (i.e. text seperated by a blank line) with <p> tags and will add <br> for other line breaks.

P.S. Change the broken vertical bars for the one on your keyboard (the forum changes it y'see).

Solta

5:06 am on May 11, 2003 (gmt 0)

10+ Year Member



Thanks a lot to both of you. Wow, what a fast response.

-Solta

andreasfriedrich

9:41 am on May 11, 2003 (gmt 0)

lorax

8:44 pm on May 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You could also use nl2br() [php.net]

>> nl2br --  Inserts HTML line breaks before all newlines in a string

daisho

2:05 am on May 14, 2003 (gmt 0)

10+ Year Member



very cool lorax. Never knew about that one. cleaner than a str_replace :)

daisho.

grahamstewart

11:51 am on May 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ahh but nl2br() doesn't insert any <p> tags, only <br>.
Whereas those 4 little preg_replaces above will convert..

A paragraph that has a
line break in it.

And another paragraph.

into

<p>A paragraph that has a <br>
line break in it.</p>
<p>And another paragraph.</p>

which is much nicer markup IMHO. :)

lorax

8:41 pm on May 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That's very true grahamstewart. I'd use it combination with some code to evaluate for paragraphs (\n\n).

andreasfriedrich

5:50 pm on May 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




>>those 4 little preg_replaces above

But why use 4 when 1 will suffice? ;-)

grahamstewart

11:45 pm on May 16, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



But why use 4 when 1 will suffice? ;-)

Readability? :p

Plus the four preg_replaces also deal with inserting <br> tags where required, which the single line solution does not.

jatar_k

5:39 am on May 17, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



andreas is right, if 1 will do then 4 is a monstrous waste of time and processing power. ;)

grahamstewart

8:39 am on May 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



True, using one massive preg_replace might be a good couple of millionths of a second faster than using four simple ones. As PHP will spend longer analysing the regular expression but will only have to scan the text once. ;)

seriously tho, this might actually be important if you are converting megs and megs of text into html at a time.

But I think for the usual application (e.g. handling of user entered text from forms) the four preg_replace solution is the way to go. :)

snappca

1:28 pm on May 17, 2003 (gmt 0)



Finally! I've been searching the web looking for someone that has had a similar problem to my own...since it seems like everyone else is not having my probs, maybe I could get a helping hand.

I've been trying to piece together a simple note taking app using a text area as the input form. The problem that I'm having though is that I don't want the data stored with html tags...I want the person to be able to go back and edit the note whenever they want, without having to remove the tags.

It seemed to me that the only way I could get the line breaks to be recognized by PHP was to escape the string in javascript first and pass that up. Since javascript escapes EVERYTHING I was a bit flustered at what I was passing. Also...my app hangs if I try to submit to long a string, so now I'm really confused since I didn't think there'd be a limit to what I could pass. This thing also doesn't allow for special characters like & # @ $ and things like that.....I'm still a newbie with PHP so I'd be real thankful if I could get some help.

Thanks

-Chris

willybfriendly

1:12 am on May 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



snappca -

trim($string);// trims white space from the beginning/end of string
striptags($string);// strips all html tags
htmlspecialchars($string);// converts special characters to html entities
addslashes($string);// escapes single quotes, double quotes and backslash characters

WBF