Jump to content

basic textarea formatting question


dazzathedrummer

Recommended Posts

Ok, again, I've been searching for the answer to this but not found anything much to solve my problem (or maybe I should say 'I found loads of info that ended up giving me a headache' haha).

 

So, all i want to do is take a form textarea, have a user input regular text that will eventually populate an area of a webpage.

 

I'm ok on everything apart from the formatting of the text - how do I convert the text being sent to the database into a format so that it comes back out of the database looking like it did when it went in (with paragraphs, line breaks, apostrophes etc).

 

Link to comment
https://forums.phpfreaks.com/topic/197347-basic-textarea-formatting-question/
Share on other sites

Use the function nl2br() http://php.net/manual/en/function.nl2br.php On the form data when you save it to the database. This converts the new lines in the from the textarea to HTML BR tags so that when it comes out of the database the page displays the new line correctly.

For apostrophes when you insert the data to the database you should use mysql_real_escape_string() http://php.net/manual/en/function.mysql-real-escape-string.php This will add slashes to the quotes ' and " to make them database friendly, strings will then be displayed like (Hello, GetPutDelete\'s awesome!) Then when you output the string use stripslashes() http://php.net/manual/en/function.stripslashes.php to remove the slashes to output (Hello, GetPutDelete's awesome).

excellent that does the trick.

 

a couple of things,

 

i'm using both functions to set variables separately (ie twice for each text input) is there a way to combine the two??

 

So, where I want text from the form feild "text" to have slashes and also have line breaks im doing this: -

 

$text1 = mysql_real_escape_string($_POST['text'];

 

then

 

$text = nl2br($text1);

 

 

and $text gets pushed into the DB.

 

just wondering if theres a more eligant way of doing that as i'll have to double the number of variables for each textarea.

 

Also - is there a way to negate nl2br? it works fine on the way into the DB, but if a user 'edits' a field using a textarea, the "<br>" tags become visible in the text area.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.