dazzathedrummer Posted April 2, 2010 Share Posted April 2, 2010 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 More sharing options...
GetPutDelete Posted April 2, 2010 Share Posted April 2, 2010 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. Link to comment https://forums.phpfreaks.com/topic/197347-basic-textarea-formatting-question/#findComment-1035853 Share on other sites More sharing options...
GetPutDelete Posted April 2, 2010 Share Posted April 2, 2010 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). Link to comment https://forums.phpfreaks.com/topic/197347-basic-textarea-formatting-question/#findComment-1035854 Share on other sites More sharing options...
dazzathedrummer Posted April 2, 2010 Author Share Posted April 2, 2010 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. Link to comment https://forums.phpfreaks.com/topic/197347-basic-textarea-formatting-question/#findComment-1036002 Share on other sites More sharing options...
xoligy Posted April 2, 2010 Share Posted April 2, 2010 Im no coder but im bettin its just $text1 = mysql_real_escape_string(nl2br($post['text'])); As for the <br> tags there is a way of removing them but im not quite sure how so pass :-P Link to comment https://forums.phpfreaks.com/topic/197347-basic-textarea-formatting-question/#findComment-1036061 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.