Glenugie Posted October 8, 2009 Share Posted October 8, 2009 Basically, I have a text area used to post announcement's on one of my pages, it writes the value to a database, and that's read on all appropriate pages. But my problem came when I wanted to be able to take line breaks in the announcements, after a bit of reading, I discovered that the Pre Tag would allow me to do this, but it doesn't quite do what I need: <pre><textarea name=content wrap=physical rows=10 cols=40 onkeyup=limiter()></textarea></pre> What that will return, in place of any line breaks is /r/n, I assume it means the same thing, but the browser doesn't read it properly, does anyone know of a way to convert /r/n to a br tag? Or another method entirely to accomplish my goal? Thanks in advance ~Glenugie~ Link to comment Share on other sites More sharing options...
Bricktop Posted October 8, 2009 Share Posted October 8, 2009 Hi Glenugie, You need to use PHP's nl2br() function. Hope this helps. Link to comment Share on other sites More sharing options...
Glenugie Posted October 8, 2009 Author Share Posted October 8, 2009 It's partially solved, but when taking a new line, it displays it with 2 new lines, so it seems to only be half working? Testing Testing Testing becomes Testing Testing Testing Thanks for the reply by the way ~Glenugie~ Link to comment Share on other sites More sharing options...
Bricktop Posted October 8, 2009 Share Posted October 8, 2009 Hi Glenugie, nl2br adds a <br> but leaves the original \n in place so in some scenarios when using hte <pre> tag you can have problems with double-spacing. There is a user-submitted (sean at boyercentral dot net) function on the manual page for the nl2br() function which will help. <?php // do nl2br except when in a pre tag function nl2brPre($string) { // First, check for <pre> tag if(!strpos($string, "<pre>")) { return nl2br($string); } // If there is a <pre>, we have to split by line // and manually replace the linebreaks with <br /> $strArr=explode("\n", $string); $output=""; $preFound=false; // Loop over each line foreach($strArr as $line) { // See if the line has a <pre>. If it does, set $preFound to true if(strpos($line, "<pre>")) { $preFound=true; } elseif(strpos($line, "</pre>")) { $preFound=false; } // If we are in a pre tag, just give a \n, else a <br /> if($preFound) { $output .= $line . "\n"; } else { $output .= $line . "<br />"; } } return $output; } ?> Hope this helps. Link to comment Share on other sites More sharing options...
Glenugie Posted October 8, 2009 Author Share Posted October 8, 2009 Worked perfectly, thanks ~Glenugie~ Link to comment Share on other sites More sharing options...
rushi1992 Posted November 18, 2014 Share Posted November 18, 2014 Hello Bricktop, I seen your code which you had mentioned above. Actually I want to copy and paste References in to my database from word file. I am trying to use textarea tag but it is showing like a paragraph so guide me how to do and what to do ? <textarea name="reference" style="width:400px; height:80px;overflow:visible;"></textarea> in database field name is "references". Link to comment Share on other sites More sharing options...
Ch0cu3r Posted November 18, 2014 Share Posted November 18, 2014 Locked. This is a 5 year old topic. @rushi1992 create a new topic explaining your problem and post the relevant code you are having issues with. Link to comment Share on other sites More sharing options...
Recommended Posts