tibberous Posted December 18, 2007 Share Posted December 18, 2007 I have a textarea, and am trying to add paragraphs and line breaks to it. Since the paragraphs have a set style in the CSS, I want to use paragraphs for all the main text, and only use line breaks if there are several spaces between a paragraph. Does anyone know how to do this? Quote Link to comment Share on other sites More sharing options...
teng84 Posted December 18, 2007 Share Posted December 18, 2007 is this upon submit? if so you might need regex and can you explain it better or maybe give sample output you want Quote Link to comment Share on other sites More sharing options...
haku Posted December 18, 2007 Share Posted December 18, 2007 echo "a line of text" . EUC . "The next line of text" This will output the following: a line of text The next line of text EUC adds a line break. Maybe this will help. Quote Link to comment Share on other sites More sharing options...
tibberous Posted December 18, 2007 Author Share Posted December 18, 2007 Yeah, it is on submit. I got it working for one and two end-lines, but I can't seem to get it to work for three. If my input is something like: Hello There I was the output to be: <p>Hello</p> <br> <p>There</p> Almost like if you types plain text into the WYSIWYG part of Frontpage / Dreamweaver, then went to view code. Here is what I have so far. I replace \r\n with \n, then replace \r with \n. That way I know \n is the only possible linebreak. function format($content){ $content = trim($content); if(strlen($content) == 0) return ""; $content = '<p>'.$content; $content = str_replace("\r\n", "\n", $content); $content = str_replace("\r", "\n", $content); $content = preg_replace("/\n{2}/", "</p>\n\n", $content); $content = preg_replace("/\n{2}/", "\n\n<p>", $content); $content = preg_replace("/[^\n]{1}\n{1}[^\n]{1}/", "<br>\n", $content); return $content.'</p>'; } Quote Link to comment Share on other sites More sharing options...
corbin Posted December 18, 2007 Share Posted December 18, 2007 http://php.net/nl2br If I understand you correctly, that should help. 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.