suttercain Posted July 30, 2007 Share Posted July 30, 2007 Hi Guys, Does anyone know a technique or code that allows a user entering text into a <textarea> to enter paragraphs without having the user enter html or the <p> tag that will be stored in MySQL then displayed to the browser. I currently use TinyMCE to do this but would like a simpler option. I don't mind if the code in the MySQL database hase the <p> tag and closing </p> tag form XHTML but I would like it to automatically be genrated if the user hits enter to space the paragraphs. EXAMPLE: How text is displayed from MySQL if the user enter spaces, they are not reflected: Taylor, along with Vick and two other co-defendants, had pleaded not guilty Thursday before U.S. District Judge Henry E. Hudson. The trial for all four defendants is scheduled for November 26. This is how I want it to look: Taylor, along with Vick and two other co-defendants, had pleaded not guilty Thursday before U.S. District Judge Henry E. Hudson. The trial for all four defendants is scheduled for November 26. So is there a way to get Paragraph breaks with the user just hitting enter and some how recognize that enter as a <p> tag? Thanks Quote Link to comment Share on other sites More sharing options...
ViN86 Posted July 30, 2007 Share Posted July 30, 2007 is the code being stored into a TEXT type field on the table? Quote Link to comment Share on other sites More sharing options...
lightningstrike Posted July 30, 2007 Share Posted July 30, 2007 Perhaps you need to look into the nl2br() function before adding it to the database. alternatively something like this could be done. $text = str_replace("\n","</p><p>",$text); $text .= "</p>"; Your problem is the textarea considers hitting the enter key as a newline character or \n Quote Link to comment Share on other sites More sharing options...
suttercain Posted July 30, 2007 Author Share Posted July 30, 2007 Yeah, Since it is longer then 255 I store it as text. Quote Link to comment Share on other sites More sharing options...
suttercain Posted July 30, 2007 Author Share Posted July 30, 2007 Hi Lightning Strike, I thought with nl2br you still have to enter /n in the code. I want the user to only have to hit enter and now html code to reflect a paragraph break. Correct me if I am wrong. Thanks Quote Link to comment Share on other sites More sharing options...
lightningstrike Posted July 30, 2007 Share Posted July 30, 2007 Textarea's create the code \n on it's own accord when a user hit's enter. \n is a newline character that the browser processes, in textarea's but not in actual html. So nl2br() is your best bet Quote Link to comment Share on other sites More sharing options...
suttercain Posted July 30, 2007 Author Share Posted July 30, 2007 Cool, I will give it a shot and click topic solved when I get it to work. Thanks guys. SC Quote Link to comment Share on other sites More sharing options...
suttercain Posted July 30, 2007 Author Share Posted July 30, 2007 Okay so I have tried a few of the nl2br functions, like this one <?php function nls2p($str) { return str_replace('<p></p>', '', '<p>' . preg_replace('#([\r\n]\s*?[\r\n]){2,}#', '</p>$0<p>', $str) . '</p>'); } ?> Then I entered this into the textarea: Hello. Test1 Test2 test3 When the above was echoed from MySQL it came out like this: Hello. Test1 Test2 test3 Also when I looked at the inout in the MySQL field it has no \n or any other tags <br /> <p>.. nada. What am I doing wrong? Thanks. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted July 30, 2007 Share Posted July 30, 2007 \n and \r characters are there but they are invisible. Also you should use double quotes when you are using escape characters: <?php function nls2p($str) { return str_replace('<p></p>', '', '<p>' . preg_replace("#([\r\n]\s*?[\r\n]){2,}#", '</p>$0<p>', $str) . '</p>'); } ?> Quote Link to comment Share on other sites More sharing options...
calabiyau Posted July 30, 2007 Share Posted July 30, 2007 http://ca.php.net/nl2br nl2br is an actual php function. you don't need a special constructed function to do it you simply put the variable name in between the parenthesis and it does it all automatically Quote Link to comment Share on other sites More sharing options...
suttercain Posted July 30, 2007 Author Share Posted July 30, 2007 Cool. Got it working. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.