Forum Moderators: coopster
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
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.
// 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).
Andreas
>> nl2br -- Inserts HTML line breaks before all newlines in a string
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. :)
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. :)
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